Get group names in java regex -


i'm trying receive both pattern & string , return map of group name -> matched result.

example:

(?<user>.*) 

i return map containing "user" key , whatever matches value.

the problem can't seem group name java regex api. can matched values name or index. don't have list of group names , neither pattern nor matcher seem expose information. have checked source , seems if information there - it's not exposed user.

i tried both java's java.util.regex , jregex. (and don't care if suggested other library good, supported & high in terms performance supports feature).

there no api in java obtain names of named capturing groups. think missing feature.

the easy way out pick out candidate named capturing groups pattern, try access named group from match. in other words, don't know exact names of named capturing groups, until plug in string matches whole pattern.

the pattern capture names of named capturing group \(\?<([a-za-z][a-za-z0-9]*)> (derived based on pattern class documentation).

(the hard way implement parser regex , names of capturing groups).

a sample implementation:

import java.util.scanner; import java.util.set; import java.util.treeset; import java.util.iterator; import java.util.regex.pattern; import java.util.regex.matcher; import java.util.regex.matchresult;  class regextester {      public static void main(string args[]) {         scanner scanner = new scanner(system.in);          string regex = scanner.nextline();         stringbuilder input = new stringbuilder();         while (scanner.hasnextline()) {             input.append(scanner.nextline()).append('\n');         }          set<string> namedgroups = getnamedgroupcandidates(regex);          pattern p = pattern.compile(regex);         matcher m = p.matcher(input);         int groupcount = m.groupcount();          int matchcount = 0;          if (m.find()) {             // remove invalid groups             iterator<string> = namedgroups.iterator();             while (i.hasnext()) {                 try {                     m.group(i.next());                 } catch (illegalargumentexception e) {                     i.remove();                 }             }              matchcount += 1;             system.out.println("match " + matchcount + ":");             system.out.println("=" + m.group() + "=");             system.out.println();             printmatches(m, namedgroups);              while (m.find()) {                 matchcount += 1;                 system.out.println("match " + matchcount + ":");                 system.out.println("=" + m.group() + "=");                 system.out.println();                 printmatches(m, namedgroups);             }         }     }      private static void printmatches(matcher matcher, set<string> namedgroups) {         (string name: namedgroups) {             string matchedstring = matcher.group(name);             if (matchedstring != null) {                 system.out.println(name + "=" + matchedstring + "=");             } else {                 system.out.println(name + "_");             }         }          system.out.println();          (int = 1; < matcher.groupcount(); i++) {             string matchedstring = matcher.group(i);             if (matchedstring != null) {                 system.out.println(i + "=" + matchedstring + "=");             } else {                 system.out.println(i + "_");             }         }          system.out.println();     }      private static set<string> getnamedgroupcandidates(string regex) {         set<string> namedgroups = new treeset<string>();          matcher m = pattern.compile("\\(\\?<([a-za-z][a-za-z0-9]*)>").matcher(regex);              while (m.find()) {                 namedgroups.add(m.group(1));             }              return namedgroups;         }     } } 

there caveat implementation, though. doesn't work regex in pattern.comments mode.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -