Question

I'm trying to extract and replace Wikipedia curly braces content with no success.

In the string below, I'd like to be able to replace {{Nihongo|Pang|パン|Pan}} by 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";

I tried many combinations of regex in my preg_replace such as the one below with no luck so far

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

Solution

Your question is not stated clearly.

If you are only wanting to replace the first occurrence of curly braces in your specific data with the second element in that group, you could use a negative lookahead to match the following comma.

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

Outputs..

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

If you want to replace every occurrence of curly braces with the second element in that group.

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

Outputs..

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

OTHER TIPS

If I understand well, you want to replace a list inside double curly brackets by the second item of the list. To do this, you can try:

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

Details:

{{          # 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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top