开发者

Dot doesn't match new lines?

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 po开发者_运维技巧ssible characters, and new lines too?


Add the s modifier, e.g.

'/./s'


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


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


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.


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:

Dot doesn't match new lines?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜