是否可以在regex中将单独的模式组组合为一个组


is it possible to combine seperate pattern groups to one group in regex

例如:

"I am living in Germany." - (I'sam)'sliving's(in'sGermany)

这提供

1 - I am
2 - in Germany

有可能从preg_match中获得字符串"I are in Germany"吗?

不能在一个匹配操作中匹配非连续文本。

相反,您可以使用preg_replace来捕获要保留的子模式周围的组,并使用backreferences在替换模式中恢复它们。

所以,使用

Regex:'~(I'sam's)living's(in'sGermany)~'
替换:''1'2'

请参阅演示。

您也可以在不使用正则表达式的情况下获得输出:

$mystr  = "I am living in Germany.";
$res = str_replace(".", "", "".join(explode("living ", $mystr)));
print $res;