user2733521
user2733521

Reputation: 449

Regexp matching 3 consecutive capital letters

i have the following text :

74 avenue Emile Counor

Bat B2 Appt B104

I want to replace all line feed, only if the following 3 letters are not 3 capitals.

For example, the previous example should become:

74 avenue Emile Counor Bat B2 Appt B104

but

74 avenue Emile Counor

BAT B2 Appt B104

should stay.

I have tried many solutions via regexp tools, but impossible to match what i want.

Here what i have tried so far

preg_replace("/\n([^A-Z]{3})/", " $1", $str)

Upvotes: 0

Views: 615

Answers (3)

user557597
user557597

Reputation:

I would use a simple approach.

Find:

 (\n[A-Z]{3})|\n  

Replace:

 $1

Further, there is no simple way to preserve/add the extra space format character here.
I mean it can be done via a callback, but adding a blind " " is wrong.

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

If you want to negate what is following the LF, the way is to use a negative lookahead:

$str = preg_replace("/\n(?![A-Z]{3})/", " ", $str);

Note that a lookahead is only a test and that its content doesn't appear in the match result.

Upvotes: 5

anubhava
anubhava

Reputation: 784998

Search regex:

'/\n(?![A-Z]{3})/'

Replacement:

" "

RegEx Demo

Code:

$str = "74 avenue Emile Counor\nBat B2 Appt B104";     
$result = preg_replace('/\n(?![A-Z]{3})/', ' ', $str);

Upvotes: 3

Related Questions