1

I'm using this regex string:

/rfi .*?(.*) .*?\((.*)\)/i

Group 1 finds the rfi number & group 2 returns the contents inside the ( ). With test string: "New RFI 087 (Concrete Beam at Planter)" everything works fine, but with test string "New RFI 087 (Concrete (Beam) at Planter)" it crashes.

Is there anyway to search inside the ( ) but ignore multiple ( ) inside of it ?

Barry Ralphs
  • 193
  • 10
  • Use `/rfi (.*?) .*?\((.*)\)/i` ([demo](https://regex101.com/r/JO29mM/1)), make your Group 1 pattern lazy. Also, the first `.*?` is redundant, remove it. – Wiktor Stribiżew Nov 14 '18 at 20:24
  • This requires a recursive regex, which engine ? –  Nov 14 '18 at 20:25
  • @sln, reclose as a dupe of https://stackoverflow.com/questions/22444/my-regex-is-matching-too-much-how-do-i-make-it-stop, the only thing OP needs is lazy quantifier with Group 1. No recursion required. No need to reopen evident duplicates. – Wiktor Stribiżew Nov 14 '18 at 20:31
  • if you have access to a PCRE or Perl style engine, use this [(\(((?:\[^()\]++|(?1))*)\))](https://regex101.com/r/ST1mfv/1). if a Java style engine, use this [(?=\()(?:(?=.*?\((?!.*?\1)(.*\)(?!.*\2).*))(?=.*?\)(?!.*?\2)(.*)).)+?.*?(?=\1)\[^(\]*(?=\2$)](https://regex101.com/r/fyVory/1) –  Nov 14 '18 at 20:33
  • `search inside the ( ) but ignore multiple ( ) inside of it` is a _RECURSION_ question _NOT_ a quantifier question. This is about _balanced text_ !! –  Nov 14 '18 at 20:40
  • Ok, posted a wiki answer. – Wiktor Stribiżew Nov 14 '18 at 20:40

1 Answers1

2

You can match your strings by making your Group 1 pattern lazy. Also, the first .*? is redundant, remove it.

Use

/rfi (.*?) .*?\((.*)\)/i

See the regex demo

@sln suggests using a regex with a subroutine, but these features are not quite popular with regex engines, Ruby Onigmo or PCRE support them, for example.

/rfi (.*?) .*?(\((?:[^()]++|(?2))*\))/i

Or a bit more "cross library" (since Onigmo supports \g<X> recursive construct, same as PCRE)

/rfi (.*?) .*?(\((?:[^()]++|\g<2>)*\))/i

See this demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • `but these features are not quite popular with regex engines` What does this mean ? –  Nov 14 '18 at 21:06