-1

どのように一致させることができます

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

「FooBar(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 に答える