Menu

List of named groups

Help
2004-05-20
2004-05-21
  • Nobody/Anonymous

    Hi Folks! 

      The named groups functionallity is exactly what I've been looking for!

      Is there any way of retrieving all the groups, or all the named groups that a match contains?  I just have 3 named groups that I'm capturing, and the rest will enter in a category called "other".  I can work around this by comparing the strings with the ones I captured. However, if one of the groups contains the same text I'm going to miss it.

      Thanks for the great work!

    PS.: do you guys have any perfomance comparisons against javax.regex?

      []s Gustavo

     
    • Sergey A. Samokhodkin

      To retrieve all the groups use the Matcher.groups() or the

      for(int i=0;i<Patterm.groupCount();i++) String group=Matcher.group(i);

      There is no way to get group names without prior knowing them.
      As a temporary workaround you can slightly change the source: add appropriate accessor methods for the Pattern.namedGroupMap hashtable, which contains the name(String)->id(Integer) pairs.

      Concerning performance.
      The jregex is slightly slower with short target strings (<80-100 chars) and faster otherwise.
      You can make it even better by reusing the Matcher objects (see docs on Matcher.setTarget()).

       
    • Nobody/Anonymous

      Thanks!

      Regarding the performance, I've noticed that, but I'm parsing large (2-16KB) multi-line strings, and it's quite faster than Sun's.  And I like your sintax for named groups better, even though it's not perl standard.

      I think I'll wait before changing the code, I'm afraid to break compatibility. 

      Again, thanks for the tips.

        []s Gustavo

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.