返回和换行符会破坏正则表达式结果

时间:2012-05-27 16:53:46

标签: php regex

我目前正在使用:

preg_match("/(.*)On\s+(Sun|Sat|Fri|Thu|Wed|Tue|Mon),\s+(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d?\d,\s+\d{4}\s+at\s+\d?\d:\d\d\s+[AP]M,/i", $to, $end);

它适用于:

$string = "I need to match this. On Sun, May 27, 2012 at 9:23 AM, Prov";

但不是为了:

$string = "I need to match this. 

On Sun, May 27, 2012 at 9:23 AM, Prov";

$ string2中的返回不允许匹配。我可以在preg_match中更改某些内容,以便返回不会破坏我的结果吗?

2 个答案:

答案 0 :(得分:7)

添加/s修饰符以允许点(.)也匹配换行符:

preg_match("/(.*)On\s+(Sun|Sat|Fri|Thu|Wed|Tue|Mon),\s+(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d?\d,\s+\d{4}\s+at\s+\d?\d:\d\d\s+[AP]M,/si", $to, $end);

答案 1 :(得分:0)

点'。'字符does not match换行符。