PHP正则表达式:从管状花括号中提取内容

时间:2013-10-22 14:15:40

标签: php regex pipe brackets curly-braces

我正在尝试提取和替换维基百科花括号内容但没有成功。

在下面的字符串中,我希望能够{{Nihongo|Pang|パン|Pan}}替换Pang

$text = "Buster Bros, also called {{Nihongo|Pang|パン|Pan}} and {{Nihongo|Pomping World|ãƒãƒ³ãƒ”ング・ワールド|Ponpingu WÄrudo|lead=yes}}, is a cooperative two-player arcade video game released in 1989 by Capcom";

我在我的preg_replace中尝试了很多正则表达式的组合,例如下面的那个没有运气到目前为止

$text = preg_replace('/\{\{({^:\|\}}+)\|({^:\}}+)\}\}/', "$2", $text);

2 个答案:

答案 0 :(得分:0)

如果我理解的话,你想要用列表的第二项替换双花括号内的列表。为此,您可以尝试:

$text = preg_replace('/{{[^|]*+\|([^|]++)(?>[^}]++|}(?!}))*+}}/', '$1', $text);

详细说明:

{{          # litteral curly brackets (no need to escape them)
[^|]*+      # first item: all that is not a `|` zero or more times
\|          # litteral `|` (must be escaped)
([^|]++)    # second item in a capture group 
(?>         # content until `}}` in a non capturing group (atomic)
    [^}]++  # all characters except `}`
  |         # OR
    }(?!})  # `}` not followed by another `}`
)*+         # repeat the group zero or more times
}}          # litteral `}}` (no need to escape them too)

答案 1 :(得分:0)

您的问题没有明确说明。

如果您只想将特定数据中第一次出现的大括号替换为该组中的第二个元素,则可以使用否定前瞻进行匹配以下逗号。

$text = preg_replace('/{{[^|]*\|([^|]++)\|[^{}]++}}(?!,)/', '$1', $text);

输出..

Buster Bros, also called Pang and {{Nihongo|Pomping World|ãƒãƒ³ãƒ”ング・ワールド|Ponpingu WÄrudo|lead=yes}}, is a cooperative two-player arcade video game released in 1989 by Capcom

如果要将每个花括号替换为该组中的第二个元素。

$text = preg_replace('/{{[^|]*\|([^|]++)\|[^{}]++}}/', '$1', $text);

输出..

Buster Bros, also called Pang and Pomping World, is a cooperative two-player arcade video game released in 1989 by Capcom