Reputation: 449
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
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
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
Reputation: 784998
Search regex:
'/\n(?![A-Z]{3})/'
Replacement:
" "
Code:
$str = "74 avenue Emile Counor\nBat B2 Appt B104";
$result = preg_replace('/\n(?![A-Z]{3})/', ' ', $str);
Upvotes: 3