将 2 个正则表达式模式与前面或后面的 1 个模式匹配
Matching 2 regex patterns with 1 pattern in the front or back
我有一个正则表达式匹配前面或后面有 1 个模式的 2 个模式,但第一个数组 return 有 2 个空索引。它为什么这样做,我如何阻止它这样做?
$text = "i did";
preg_match("~(?:(did) (.+)|(.+) (did))~", $text, $match);
print_r($match);
echo "<br>";
$text = "did i";
preg_match("~(?:(did) (.+)|(.+) (did))~", $text, $match);
print_r($match);
结果:
Array ( [0] => i did [1] => [2] => [3] => i [4] => did )
Array ( [0] => did i [1] => did [2] => i )
想要的结果:
Array ( [0] => i did [1] => i [2] => did )
Array ( [0] => did i [1] => did [2] => i )
您可以使用 branch reset (?|...)
:
Alternatives inside a branch reset group share the same capturing groups. The syntax is (?|regex)
where (?|
opens the group and regex is any regular expression.
您的 preg_match
看起来像:
preg_match("~(?|(did) (.+)|(.+) (did))~", $text, $match);
结果:
Array
(
[0] => i did
[1] => i
[2] => did
)
我猜你的正则表达式是一个示例。如果您需要匹配 did
之后或之前的单词,请使用 \w
shorthand class:
preg_match("~(?|(did) (\w+)|(\w+) (did))~", $text, $match);
这是一个修改后的版本,可以按照您的意愿运行:
$text1 = "i did";
preg_match("~(did|\w+(?= did)) (did|(?<=did )\w+)~", $text1, $match1);
print_r($match1);
$text2 = "did i";
preg_match("~(did|\w+(?= did)) (did|(?<=did )\w+)~", $text2, $match2);
print_r($match2);
$text3 = "did x, x did";
preg_match_all("~(did|\w+(?= did)) (did|(?<=did )\w+)~", $text3, $match3);
print_r($match3);
$text4 = "a a";
preg_match("~(did|\w+(?= did)) (did|(?<=did )\w+)~", $text4, $match4);
print_r($match4);
在线版本here
注意:正则表达式利用了正则表达式中OR的行为,第一个匹配的结果停止正则表达式引擎走得更远。
我有一个正则表达式匹配前面或后面有 1 个模式的 2 个模式,但第一个数组 return 有 2 个空索引。它为什么这样做,我如何阻止它这样做?
$text = "i did";
preg_match("~(?:(did) (.+)|(.+) (did))~", $text, $match);
print_r($match);
echo "<br>";
$text = "did i";
preg_match("~(?:(did) (.+)|(.+) (did))~", $text, $match);
print_r($match);
结果:
Array ( [0] => i did [1] => [2] => [3] => i [4] => did )
Array ( [0] => did i [1] => did [2] => i )
想要的结果:
Array ( [0] => i did [1] => i [2] => did )
Array ( [0] => did i [1] => did [2] => i )
您可以使用 branch reset (?|...)
:
Alternatives inside a branch reset group share the same capturing groups. The syntax is
(?|regex)
where(?|
opens the group and regex is any regular expression.
您的 preg_match
看起来像:
preg_match("~(?|(did) (.+)|(.+) (did))~", $text, $match);
结果:
Array
(
[0] => i did
[1] => i
[2] => did
)
我猜你的正则表达式是一个示例。如果您需要匹配 did
之后或之前的单词,请使用 \w
shorthand class:
preg_match("~(?|(did) (\w+)|(\w+) (did))~", $text, $match);
这是一个修改后的版本,可以按照您的意愿运行:
$text1 = "i did";
preg_match("~(did|\w+(?= did)) (did|(?<=did )\w+)~", $text1, $match1);
print_r($match1);
$text2 = "did i";
preg_match("~(did|\w+(?= did)) (did|(?<=did )\w+)~", $text2, $match2);
print_r($match2);
$text3 = "did x, x did";
preg_match_all("~(did|\w+(?= did)) (did|(?<=did )\w+)~", $text3, $match3);
print_r($match3);
$text4 = "a a";
preg_match("~(did|\w+(?= did)) (did|(?<=did )\w+)~", $text4, $match4);
print_r($match4);
在线版本here
注意:正则表达式利用了正则表达式中OR的行为,第一个匹配的结果停止正则表达式引擎走得更远。