tags:

views:

83

answers:

5

When I write a regex with . in it, it doesn't match new lines.

preg_match('/.*+?/') ...

What do I need to write, to match all possible characters, and new lines too?

+8  A: 

Add the s modifier, e.g.

'/./s'
KennyTM
+3  A: 

By default . will not match newlines. You can change this behaviour with the s modifier.

Cocowalla
A: 

Apart from the s modifier you should think about using a negated character class. Instead of

 #http://example\.org/.+/.+/.+#

you may use

 #http://example\.org/[^/]+/[^/]+/[^/]+#

which ought to be faster.

nikic
+1  A: 

The . does not match new lines - and that is on purpose (though I am not really sure why). You would use the s modifier to change this behaviour, and make . match all characters, including the newline.

Example:

$text = "Foobar\n123"; // This is the text to match

preg_match('/^Foo.*\d+$/', $text); // This is not a match; the s flag isn't used
preg_match('/^Foo.*\d+$/s', $text); // This is a match, since the s flag is used
Frxstrem
A: 

Try this:

   \r : carriage return
   \n : new line 
   \w : even [a-zA-Z0-9_] 
   *  : 0 or more. even :  +?
   $text = "a\nb\n123"; 
   preg_match("/[\w\r\n]*/i", $text, $match);
   print_r($match);

vide this list:

http://i26.tinypic.com/24mxgt4.png

Jet
That says `<` and `>` are metacharacters and have to be escaped with a backslash if you want to match them literally. It also says `\<` and `\>` match word boundaries (start and end, respectively). Can you say "Catch-22"? Fortunately for us, neither of those things is actually true in PHP (`preg_*`) regexes. ;)
Alan Moore