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\/)+?.*/';
精彩评论