Frage

I have a regex with multiple disjunctive capture groups

(a)|(b)|(c)|...

Is there a faster way than this one to access the index of the first successfully matching capture group?

(matcher is an instance of java.util.regex.Matcher)

int getCaptureGroup(Matcher matcher){
    for(int i = 1; i <= matcher.groupCount(); ++i){
        if(matcher.group(i) != null){
            return i;
        }
    }
}
War es hilfreich?

Lösung

That depends on what you mean by faster. You can make the code a little more efficient by using start(int) instead of group(int)

if(matcher.start(i) != -1){

If you don't need the actual content of the group, there's no point trying to create a new string object to hold it. I doubt you'll notice any difference in performance, but there's no reason not to do it this way.

But you still have to write the same amount of boilerplate code; there's no way around that. Java's regex flavor is severely lacking in syntactic sugar compared to most other languages.

Andere Tipps

I guess the pattern is so:

if (matcher.find()) {
  String wholeMatch = matcher.group(0);
  String firstCaptureGroup = matcher.group(1);
  String secondCaptureGroup = matcher.group(2);
  //etc....
}

There could be more than one match. So you could use while cycle for going through all matches.

Please take a look at "Group number" section in javadoc of java.util.regex.Pattern.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top