[英]Regex/PHP Replace any repeating word group
如何搭配
$string = "Foo Bar (Any Group - ANY GROUP Baz)";
应返回为“ Foo Bar(Any Group-Baz)”
是否可以在没有bruteforce的情况下进行替换 ?
编辑:*该组可以由1-4个单词组成,而每个单词可以匹配[A-Za-z0-9\\/\\(\\)]{1,30}
*分隔符始终为-
将空格排除在允许的“单词”字符列表之外,以下示例适用于您:
$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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.