0

This question might have been asked before, but I couldn't find this particular version of the problem.

I want to match everything inside parentheses, which is easy in itself. But my problem is that the text inside of the parentheses can have nested parentheses and text strings containing parentheses!

Look at this example : \((.*)\)

  1. ('1KtxNawkosV1H5jzUzW_$1'),#38,'2ndLevel', '2a',#251524,#251773,#328411,.PHYSICAL.,.EXTERNAL.

  2. '1KtxNawkosV1H5jzUzW_$1',#38,'2ndLevel', '2a',#251524,#251773,(#328411,.PHYSICAL.,.EXTERNAL.)

  3. ('1KtxNawkos(asd)V1H5jzU)zW_$1', ('asd', #23331), #21)<-- ###should end here###,#38,'2ndLevel', '2a',#251524,#251773,#328411,.PHYSIaCAL.,.EXTERNAL., ')'

  4. #38,'2ndLevel',('1KtxNawkos(asd)V1H5jzU)zW_$1') '2a',#251524,#251773,#328411,.PHYSICAL.,.EXTERNAL.

It's almost doing what I need, but it fails in the 3rd example.

It should basically omit parentheses inside (in this case, single) quotes and find the outer most matching parentheses.

What do I need to change in order to make it work?

Thanks!

mplungjan
  • 169,008
  • 28
  • 173
  • 236
mottosson
  • 3,283
  • 4
  • 35
  • 73
  • You can’t count parentheses with regular regular expressions, or even JavaScript’s extended regular expressions AFAIK. – Ry- Feb 06 '17 at 18:22
  • 1
    duplicate of http://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns ? – mplungjan Feb 06 '17 at 18:23
  • not quite, since I want to exclude parens within a string. But maybe I'm asking for too much. Maybe regex is not the solution to this problem...? – mottosson Feb 06 '17 at 18:30

1 Answers1

0

Your issue with #3 which you will have trouble solving with regexes is that you have a nested expression in parentheses which is NOT contained in a string. I understand that you want to ignore parentheses in strings, which is fine and doable with regex but that's not the case in #3.

Your proposed regex will match the first '(' followed by the closest ')' to the next newline and EVERYTHING in between, including other ')' whether they are inside quotes or not. If you want to exclude brackets in single-quoted strings, consider incorporating the following pattern:

'[^']*'

That is, a single-quote, followed by anything but a quote (including newlines and parentheses, followed by a closing single-quote.

I hope this was somewhat helpful.

jakeehoffmann
  • 1,364
  • 14
  • 22