[英]PHP Regex missing capture group
I'm trying to parse several coordinates, but PHP preg_match is missing a capture group. 我正在尝试解析几个坐标,但是PHP preg_match缺少捕获组。 It works in every regex simulator I've tested, but not in my code.
它可以在我测试过的每个正则表达式模拟器中使用,但不适用于我的代码。 Group 4 always comes up empty;
第4组总是空着;
$coordinates = 'N40.765775° E8.303369°';
// -40.765775° -8.303369°
// 40.765775° 8.303369°
// -40.765775° 8.303369°
// N40.765775° E8.303369°
// S40.765775° E8.303369°
// N40.765775° W8.303369°
// S40.765775° W8.303369°
$regex = '/([-NnSs]?)\D*([0-9]?[0-9])\.(\d{1,10}+)\D\h*([-EeWw]?)\D*([0-1]?[0-9]?[0-9])\.(\d{1,10}+)/';
if (preg_match($regex, $coordinates, $matches)) {
print_r($matches);
} else {
echo "no matches";
}
Since you are parsing strings that have multibyte characters (°), you should add the unicode modifier , ie u
at the end of the regex. 由于您要解析具有多字节字符(°)的字符串,因此应在regex末尾添加unicode 修饰符 ,即
u
。
/ /u
Just as an illustration: without that modifier, if you would add a .
就像一个例子:如果没有该修饰符,则添加
.
after \\D
, here: 在
\\D
,在这里:
\D.\h*([-EeWw]?)
... then you would capture the "E" in the fourth capture group. ...那么您将在第四个捕获组中捕获“ E”。 So
\\D.
所以
\\D.
actually matches the multibyte character °
. 实际上匹配多字节字符
°
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.