开发者

Regular expression: do not include some parenthesized subpattern in matched groups

开发者 https://www.devze.com 2023-01-26 19:51 出处:网络
For example, for the text Mon 01/01/2010 01:00:00 some other text followed, I want to match the date segment only, so I use the following PHP code,

For example, for the text Mon 01/01/2010 01:00:00 some other text followed, I want to match the date segment only, so I use the following PHP code,

$text = 'Mon 01/01/2010 01:00:00 some other text followed';
$pattern = '/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s(0-9\/)+?.*/';
preg_match($pattern, $text, $matches);
$date = $matc开发者_C百科hes[2];

Is it possible not to include the first parenthesized subpattern (day of week segment in this example) in matched groups, so that we can get the date segment with $matches[1] instead of $matches[2]? I remember we can do this by setting something in the regex pattern but I could not google out the answer.


Using (?:...) instead of (...) will create a non-grouping match.

$pattern = '/(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s(0-9\/)+?.*/';
0

精彩评论

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