This discussion is archived
2 Replies Latest reply: Dec 4, 2012 9:07 AM by 977703 RSS

how to get the offset using the named-capturing-group in Java 7?

977703 Newbie
Currently Being Moderated
Hi all,

Java 7 provides the named-capturing group in the Regex Matcher class, but it misses several methods which returns the offset associated with the named-capturing group. At least, the following two methods:

1) int      end(String name);
Returns the offset after the last character of the subsequence captured by the given named-capturing group during the previous match operation.
2) int      start(String name);
Returns the start index of the subsequence captured by the given named-capturing group during the previous match operation.

Since the above two methods do not exist in current Java 7, the second print line in the following sample does not work. Anyone has suggestion to make it work?

String match ="aaabbbaaa";          
          String pattern = "(?<AString>a+)b*a*"; // The "AString" is a named-capturing group
          Pattern patten = Pattern.compile(pattern);          
          Matcher matcher = patten.matcher(match);                    
          int groupID = 1; //In this example, We know the "AString" has group ID = 1, but most time, we don't know the group ID of the named-capturing group.
                    
          if (matcher.matches()) {
               
// When we know the group ID, it is easy to use,
               System.out.println(" Found the text \"" + matcher.group(groupID) + "\" at the index " +
                         matcher.start(groupID) + " and end at index " + matcher.end(groupID) );               
               
               // The following line does not work
               System.out.println(" Found the text \"" + matcher.group("AString") + "\" at the index " +
                         matcher.start("AString") + " and end at index " + matcher.end("AString") );               
                              
          }

Thanks a lot.

Tao
  • 1. Re: how to get the offset using the named-capturing-group in Java 7?
    sabre150 Expert
    Currently Being Moderated
    taoqian wrote:Anyone has suggestion to make it work?
    I'm at a loss as to what you expect the members of this forum to do. The Matcher class does not have the two methods you desire and you already have a work-around in that you can use the numeric group id. Now you could raise a bug report suggesting a change but at best this is likely to take years to be implemented.

    Though I can't really see the advantage and would seem to go against the spirit of using group names, if you absolutely need to use the name of a group then you could create a map between a group name and it's index.
  • 2. Re: how to get the offset using the named-capturing-group in Java 7?
    977703 Newbie
    Currently Being Moderated
    @sabre150, you are right that I am looking for extra methods in the Matcher class.

    The reason is that there is no direct map between the named-group and its numeric group id in the current version, and it is difficult to create the map between these two.

    In my sample,
    System.out.println(matcher.group("AString")) will display "aaa", but it is impossible to know it is the "aaa" at the beginning of the match string or the "aaa" at the end of the match string.

    Thanks for you comments. I will take you advice to submit a bug report.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points