Regex: split in parentheses ignore nested parentheses inside quotes

My program parses a multi-line SQL VALUES string in a single-line array of strings.

A typical input line is as follows:

(11,'-1','Service A (nested parentheses)','en') (22,'-2','Service B (nested parentheses)','en')

Required Conclusion:

  • group 1: 11,'-1','Service A (nested parentheses)','en'
  • group 2: 22,'-2','Service B (nested parentheses)','en'

I tried using regexp with partial luck:

\(('.*?'|.*?)\)

What would be the correct way to handle this in regexp?

EDIT:

  • Target platform is Java 6/7
  • No need to replace parentheses with a new line - only for capturing groups
+4
source share
3 answers

EDIT: After your comment on emoticons, I suggest an alternative approach:

(?<=\()(?:'[^']*'|[,\s]+|\d+)+(?=\))

. . , , , . ?

, Java:

(?<=\()(?:[^()]+|\([^)]+\))+

?

  • Lookbehind , (
  • + : (i) , , OR | (ii) full (parenthesized expressions)

, , :

(?<=\()(?:[^()]+|\([^)]+\))+(?=\))
+1
pattern.compile("\\(((?:'[^']*'|[^'\\(\\)]+)+)\\)");

RegexPlanet Java.

'[^']*'|[^'\(\)] - , , . , , Casimir et Hippolyte, ( Java).

+1

:

/\(.*\)/\1/

/\) \(/\r/g

,

:

  • , ,
  • ,
0

Source: https://habr.com/ru/post/1541836/


All Articles