-1

怎么能配

$string = "Foo Bar (Any Group - ANY GROUP Baz)";

应返回为“Foo Bar (Any Group - Baz)”

没有暴力是否可以像这里那样替换字符串中的重复字符串

编辑:*该组可以由 1-4 个单词组成,而每个单词可以匹配[A-Za-z0-9\/\(\)]{1,30} *分隔符始终是-

4

1 回答 1

5

在允许的“单词”字符列表中留出空格,以下内容适用于您的示例:

$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);
于 2012-05-22T20:50:10.950 回答