0

Am trying to capture repeated group, for the following string.

... and abc ( xyz(d.e),'f','g','h','i',xyz('p/q'),'r') = u...

and i do expect the output to be grouped in different matches in the below format

xyz(d.e)
'f'
'g'
'h'
'i'
xyz('p/q')
'r'

The closest solution i could find was this https://stackoverflow.com/a/65244969 . So i did try with

abc\s*((?:([^,]+),?)

and got the output as

xyz(d.e)

However when i do try with

abc\s*((?:([^,]+),?)+

I get

'r') = u...

Any suggestions?

  • 1
    Try `(?:\G(?!\A),|abc\s*\()\s*(\w+\('.*?'\)|'[^']*'|[^,']+)` and grab Group 1 values, see [this demo](https://regex101.com/r/SgSN5e/1). – Wiktor Stribiżew Jun 09 '22 at 09:57
  • Is your expectation correct? I think it should probably be `abc('p/q')` instead of `xyz('p/q')` correct? – dpr Jun 09 '22 at 11:26

1 Answers1

0

In your example strings you have either a part between single quotes or a part starting with word characters followed by parenthesis.

As your tried patterns start with abc, you might use:

(?:abc\s*\(\s*|\G(?!^),)('[^',]*'|\w+\([^()]*\))

Explanation

  • (?: Non capture grouop
    • abc\s*\(\s* Match abc a space followed by ( between optional whitepace chars
    • | Or
    • \G(?!^), Assert the position at the end of the previous match, but not at the start of the string to get repetitive matches. Following by matching a comma.
  • ) Close the non capture group
  • ( Capture group 1
    • '[^',]*' Match from '...' without matching ' or a comma in between
    • | Or
    • \w+\([^()]*\) Match 1+ word chars followed by (...)
  • ) Close group 1

Regex demo | Java demo

Example code getting the group 1 value:

String regex = "(?:abc\\s*\\(\\s*|\\G(?!^),)('[^',]*'|\\w+\\([^()]*\\))";
String string = "and abc ( xyz(d.e),'f','g','h','i',abc('p/q'),'r') = u";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    if (null != matcher.group(1)) {
        System.out.println(matcher.group(1));
    }
}

Output

xyz(d.e)
'f'
'g'
'h'
'i'
abc('p/q')
'r'
The fourth bird
  • 154,723
  • 16
  • 55
  • 70