如何匹配重复为“ANY GROUP”或“ANYGROUP”的“Any Group”
$string = "Foo Bar (Any Group - ANY GROUP Baz)
Foo Bar (Any Group - ANYGROUP Baz)";
所以他们以“Foo Bar (Any Group - Baz)”的形式返回
分隔符总是-
这篇文章扩展了Regex/PHP 替换任何重复的词组
这匹配“Any Group - ANY GROUP”,但没有空格重复时不匹配。
$result = preg_replace(
'%
( # Match and capture
(?: # the following:...
[\w/()]{1,30} # 1-30 "word" characters
[^\w/()]+ # 1 or more non-word characters
){1,4} # 1 to 4 times
) # End of capturing group 1
([ -]*) # Match any number of intervening characters (space/dash)
\1 # Match the same as the first group
%ix', # Case-insensitive, verbose regex
'\1\2', $subject);