3

I was replacing blank lines for a <p> tag. The regex replace all blacklines allowing white-spaces (\s), for one <p> tag.

For example this string:

$string="with.\n\n\n\nTherefore";

But return 2 <p> tags.

So, i've done this test:(It's not for replace

, just for test)

$string="with.\n\n\n\nTherefore";
$string=preg_replace('/(^)(\s*)($)/m','[$1]|$2|($3)',$string);
echo $string;

And check what's return:

with.
[]|

|()[]||()
Therefore

Imagining:

with.\n
^\n
\n
$^\n$\n
Therefore

The regex add one \n, and the 4th one does not do what 'she' have to do.(jump to the another line).

Someone that can help. basically explain rather than solve the problem. Thanks evryone.

nEAnnam
  • 1,246
  • 2
  • 16
  • 22

1 Answers1

4

Your regex should match at least one whitespace character. So replace \s* with \s+ or if it needs escaping \s\+


\s* will match every single character, that's because it matches any(*) whitespace(\s), and because of any it includes none. By none I mean that in the string 'abc' \s* will match the "empty" characters between '^' and 'a', 'a' and 'b', 'b' and 'c', 'c' and '$'.

It is really easy to test on a linux terminal like this:

$ echo "abc" | sed 's:\s*:\n:g'  # replace \s* with \n for the whole string 

a
b
c

$ # ^ the result

As you can see it matches every "empty" character and replaces it with \n

On the other hand \s+ will force the regex to match at least 1(+) whitespace(\s) character, so it works as excpected.

c00kiemon5ter
  • 16,994
  • 7
  • 46
  • 48
  • Ok thanks, it is usefull, know u one book that can explain how the regex engine works? – nEAnnam Jun 19 '11 at 19:16
  • 1
    Please, check these two questions: [`1 ~ learning regular expressions`](http://stackoverflow.com/questions/4736/learning-regular-expressions) and [`2 ~ good starting point for regex`](http://stackoverflow.com/questions/3046457/good-starting-point-to-learn-regular-expressions) – c00kiemon5ter Jun 19 '11 at 19:20