如何匹配
$string = "Foo Bar (Any Group - ANY GROUP Baz)";
应该返回“Foo Bar(Any Group - Baz)”
是否可以在没有强力的情况下Replace repeating strings in a string?
编辑:
*该组可以包含1-4个单词,而每个单词可以匹配[A-Za-z0-9\/\(\)]{1,30}
*分隔符始终为-
答案 0 :(得分:4)
将空格从允许的“单词”字符列表中删除,以下内容适用于您的示例:
$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);