[英]PHP regexp values
My Regexp: 我的正则表达式:
/^(\p{L}+)(?:\.(\p{L}+))*$/
and subject: 和主题:
app.config.db
It returns matches app
and db
. 它返回匹配的
app
和db
。 Why this script omit config
? 为什么此脚本忽略
config
?
Because of this part: 由于这一部分:
(?:\.(\p{L}+))*
You're repeating a capturing group, which will only ever capture the last capture. 您要重复一个捕获组,该捕获组只会捕获最后一个捕获。 You might want to look into
preg_match_all()
, which (with a different regex) would be able to return all of the matches. 您可能需要研究
preg_match_all()
,它(使用不同的正则表达式)将能够返回所有匹配项。
Example: 例:
$input = 'app.config.db';
$regex = '/\.?(\p{L}+)/';
preg_match_all($regex, $input, $matches);
print_r($matches[1]);
Output: 输出:
Array
(
[0] => app
[1] => config
[2] => db
)
Your regex contains two capturing groups, so it will capture two strings. 您的正则表达式包含两个捕获组,因此它将捕获两个字符串。 The first group (
^(\\p{L}+)
) captures app
because it's at the beginning of the string. 第一组(
^(\\p{L}+)
)捕获app
因为它位于字符串的开头。
The second group initially captures config
. 第二组最初捕获
config
。 But it's nested inside a *
, so it repeats and captures db
. 但是它嵌套在
*
,因此它重复并捕获db
。 In general, the result will be the last string captured by any group. 通常,结果将是任何组捕获的最后一个字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.