REGEXP对特殊字符返回false [英] REGEXP returning false on special characters

查看:171
本文介绍了REGEXP对特殊字符返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在regexp上不太好,但是希望有人可以向我更好地解释,我在调试的代码中发现了这一点.我不知道为什么在这种情况下我总是会犯错误.

I'm not too good in regexp but hoping someone could explain better to me, I found this in the code that I debug. I wonder why I always got false on this scenario.

我知道\p{L}匹配类别字母"中的单个代码点. 0-9是数字.

I know \p{L} matches a single code point in the category "letter". 0-9 is numeric.

$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字符.

But if the value is 18 February 2015, it will print int(1). Why is that so? It is suppose to return 1 in both values because \p{L} will accept unicode characters.

推荐答案

找出解决方法,请使用/u代替/i.

Figured out a fix, use /u instead of /i.

$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*$/u", "18 Février 2015");
var_dump($match); //It will print int(1).

感谢所有帮助

这篇关于REGEXP对特殊字符返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆