PHP正则表达式:匹配一堆术语而不记录匹配

时间:2011-09-05 07:37:59

标签: php regex preg-match

我猜的是一个相当基本的正则表达式问题:

// Example strings

$string = "test test 24/11/2009";
$string = "test test 21-11-09";
$string = "test testtest 24.11.2009test";

$delim = "(\.|-|/)";
// dd/mm/yyyy
preg_match("#[^0-9](\d{1,2})$delim(\d{1,2})$delim(20\d{2})[^0-9]#i", $string, $result);
// dd/mm/yy
preg_match("#[^0-9](\d{1,2})$delim(\d{1,2})$delim(\d{2})[^0-9]#i", $string, $result);

现在我想匹配分隔符而不将它们显示在$ result数组中。

2 个答案:

答案 0 :(得分:4)

分隔符是“。”,“ - ”,“/”中的一个:并且它们不包含在$ result中,因为它们不在“()”中

$delim = "[./-]+";

答案 1 :(得分:1)

非捕获组应该完成这项工作:

$delim = "(?:\.|-|/)";