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

PHP正则表达式:从带管道的花括号中提取内容,php,regex,pipe,brackets,curly-braces,Php,Regex,Pipe,Brackets,Curly Braces,我试图提取并替换维基百科的花括号内容,但没有成功 在下面的字符串中,我希望能够将替换为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

我试图提取并替换维基百科的花括号内容,但没有成功

在下面的字符串中,我希望能够将
替换为
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_替换中尝试了许多正则表达式组合,例如下面的一个,但迄今为止运气不佳

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

如果我理解的很好,您希望用列表的第二项替换双花括号内的列表。为此,您可以尝试:

$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)

你的问题没有说清楚

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

$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
Buster Bros, also called Pang and Pomping World, is a cooperative two-player arcade video game released in 1989 by Capcom
如果要用该组中的第二个元素替换出现的每个大括号

$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
Buster Bros, also called Pang and Pomping World, is a cooperative two-player arcade video game released in 1989 by Capcom

您想用
Pang
只替换第一组括号,还是用该组中的第二个单词替换所有括号?你需要清楚地说明这一点。哇,非常感谢你提供了这两个。这是我需要的第二个。很好,现在我将进一步研究它(从PHP/FI 2开始就一直在做PHP,但仍然有麻烦),很高兴我能提供帮助。如果您还有其他问题,请告诉我。我可能会,现在正在尝试从相同页面中删除不同的内容:D