我不太擅长正则表达式,但希望有人能更好地向我解释,我在调试的代码中发现了这一点。我想知道为什么我在这种情况下总是出错。
我知道\p{L}
匹配“字母”类别中的单个代码点。0-9
是数字。
$regExp = /^\s*
(?P([0-2]?[1-9]|[12]0|3[01]))\s+
(?P\p{L}+?)\s+
(?P[12]\d{3})\s*$/i;
$value = '12 Février 2015' ;
$matches = array();
$match = preg_match($regExp, $value, $matches);
附加信息,我想出了这个:
$match = preg_match("/^\s*(?P<monthDay>([0-2]?[1-9]|[12]0|3[01]))\s+(?P<monthNameFull>\p{L}+?)\s+(?P<yearFull>[12]\d{3})\s*$/i", "18 Février 2015");
var_dump($match); //It will print int(0).
但如果值为18 February 2015
,它将打印 int(1)。为什么呢?假设在两个值中都返回 1,因为\p{L}
将接受 unicode 字符。