正则表达式/ PHP替换任何重复(但灵活)的单词组

时间:2012-11-03 15:14:39

标签: php regex

我如何匹配"任何组"重复为"任何组"或" ANYGROUP"

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

所以他们返回" Foo Bar(Any Group - Baz)"

分隔符始终为-

这篇文章扩展了Regex/PHP Replace any repeating word 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);

2 个答案:

答案 0 :(得分:1)

这很难看(正如我所说的那样),但它应该有效:

$result = preg_replace(
    '/((\b\w+)\s+)               # One repeated word
    \s*-\s*
    \2
    |
    ((\b\w+)\s+(\w+)\s+)         # Two repeated words
    \s*-\s*
    \4\s*\5
    |
    ((\b\w+)\s+(\w+)\s+(\w+)\s+) # Three
    \s*-\s*
    \7\s*\8\s*\9
    |
    ((\b\w+)\s+(\w+)\s+(\w+)\s+(\w+)\s+)  # Four
    \s*-\s*
    \11\s*\12\s*\13\s*\14\b/ix', 
    '\1\3\6\10-', $subject);

答案 1 :(得分:0)

最多6个字的解决方案是:

$result = preg_replace(
    '/
     (\(\s*)
     (([^\s-]+)
      \s*?([^\s-]*)
      \s*?([^\s-]*)
      \s*?([^\s-]*)
      \s*?([^\s-]*)
      \s*?([^\s-]*))
     (\s*\-\s*)
     \3\s*\4\s*\5\s*\6\s*\7\s*\8\s*
     /ix',
     '\1\2\9',
     $string);

检查 this demo