4

I have a regex pattern which matches different expressions but i want both matches to be in the same capturing group. Currently i got the following regex:

-([^\s"]+)|-"(.+?)"

This does in fact match both (-hello -"Hello World") but in different groups (-hello = group1 and "Hello World" = group2).

In fact I got a working example, which is unfortunately not possible in Java regex:

(?|-([^\s"]+)|-"(.+?)")
Joel
  • 138
  • 10

2 Answers2

4

One option is to use look around assertion so you can get rid of all capture groups and get full match in result:

(?<=-)[^\s"]+|(?<=-")[^"]*(?=")

RegEx Demo

Pattern p = Pattern.compile("(?<=-)[^\\s\"]+|(?<=-\")[^\"]*(?=\")");

List<String> res = p.matcher("(-hello -\"Hello World\")")
        .results()
        .map(MatchResult::group)
        .collect(Collectors.toList());

//=> [hello, Hello World]
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

Just use a regex pattern matcher and check both capture groups, using whichever one is not null:

String input = "-hello -\"Hello World\"";
String pattern = "-([^\\s\"]+)|-\"(.+?)\"";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
while (m.find()) {
    String match = !Objects.isNull(m.group(1)) ? m.group(1) : m.group(2);
    System.out.println("Found a match: " + match);
}

This prints:

Found a match: hello
Found a match: Hello World
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360