2

Using:

$text = preg_replace("/\[\[(.*?)SPACE(.*?)\]\]/im",'$2',$text);

for cleaning and get wordtwo

$text = '..text.. [[wordoneSPACE**wordtwo**]] ..moretext..';

but fails if text has [[ before

$text = '.. [[ ..text(not to cut).. [[wordoneSPACE**wordtwo**]] ..moretext..';

how can I limit to only where I have only the SPACE word?

Pedro P
  • 373
  • 1
  • 4
  • 16

2 Answers2

1

If there can be no [ and ] inside the [[...]] you may use

$text = preg_replace("/\[\[([^][]*)SPACE([^][]*)]]/i",'$2',$text);

See the regex demo. [^][] negated character class will only match a char other than [ and ] and won't cross the [[...]] border.

Otherwise, use a tempered greedy token:

$text = preg_replace("/\[\[((?:(?!\[{2}).)*?)SPACE(.*?)]]/is",'$2',$text);

See this regex demo.

The (?:(?!\[{2}).)*? pattern will match any char, 0 or more repetitions but as few as possible, that does not start [[ char sequence, and won't cross the next entity [[ border.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

Another option might be using possessive quantifiers.

In the first group you could use a negated character class to match any characters except square brackets or an S if it is followed by SPACE.

\[\[([^][S]++(?:S(?!PACE)|[^][S]+)*+)SPACE([^][]++)\]\]

In parts

  • \[\[ Match [[
  • ( Capture group 1
    • [^][S]++ Match 1+ times any char except S, ] or [
    • (?: Non capturing group
      • S(?!PACE) Match either an S not followed by PACE
      • | Or
      • [^][S]+ Match 1+ times any char except S, ] or [
    • )*+ Close group and repeat 0+ times
  • ) Close group 1
  • SPACE Match literally
  • ( Capture group 2
    • [^][]++ Match 1+ times any char except ] or [
  • ) Close group
  • \]\] Match ]]

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70