Menu

finding the sub expression index

Help
2003-08-20
2004-05-21
  • saktheesh p

    saktheesh p - 2003-08-20

    How can i find the sub expression indes of a matched string i.e

    if i get a matcher class how do i know which sub expression of the regular expression index it belonged to ?

    p.s : i want to find it form the matcher class and not by matching a particular group in that regular expression pattern pattern

     
    • Sergey A. Samokhodkin

      Can't understand the question.
      Elaborate please. Examples are welcome.

       
      • saktheesh p

        saktheesh p - 2003-08-21

        say i concatenate 5 regular expression with "or"
        i.e the regular expression is like  "re1|re2|re3|re4|re5"

        and i have a string say abcabcabcacb( for example)

        if i give matcher.findAll() it retuns me a match iterator

        now i need to find from the matcher class/or by some means the regular expression that matched the string ( p.s: i cannot use the match group thing cause i cannot check for each group whether it matched)

         
        • Sergey A. Samokhodkin

          There is no straightforward way to do it.
          There still exists a crappy workaround: wrap each RE in a named group and check each one whether it's captured:

          String[] reArray=..;
          StringBuffer sb=new StringBuffer();
          for(int i=0;i<reArray.length;i++){
             sb+="({g"+i+"}"+re[i]+")";
             if(i<(reArray.length-1)) sb+="|";
          }
          Pattern p=new Pattern(sb.toString());

          Matcher m=p.matcher(target);
          if(m.find()){
            for(int i=0;i<reArray.length;i++){
              if(m.isCaptured("g"+i)) return i;
            }
          }

          Basically, it's easy to implement the functionality you look for. But how the API may look like?

           
          • saktheesh p

            saktheesh p - 2003-08-21

            i have a problem i doin this i need to identify it from the matcher class is there any way that i can access the sub expression index form it , ( at least by tweaking the package a little like extending the scope to public etc ..) is there any way to do this

             
            • Sergey A. Samokhodkin

              Missed the point again. Identify what? The "the sub expression index" is too ambiguous. Give examples, please.

               
              • saktheesh p

                saktheesh p - 2003-08-22

                thats what  detailed in my previous reply say i have n sub expression combined by r an sub expression  x may have subexpressions in it .

                now i have a set of string i match the string and i get the results now i need to which subexpression matched the particular part of the string WITHOUT MAKING ANY PREOPERATIONS IN THE REGULAR EXPRESSIONS( like adding a group name)

                e.g

                regular exp : "((h|o)|(hi[m])|(\([^(][^)])|(r+))"

                match string:  "i told him"

                say i get a match "him" in the match result class is there any way for me to find the sub expression index of the part (hi[m]) i.e where in the regular expression that the string matched even the starting index of it will do e.g (hi[m]) starting index 7 from the start of the regular exprssion <or> the Nth group i.e if i get the him matched 2nd group

                i hope i have given it clear

                 
                • Sergey A. Samokhodkin

                  Ok, what you actually need is a kind of namespaces, am I right?
                  Something like we take two REs, each with groups 1..3, somehow setup a namespace for each RE, then merge them and have groups 1..3 in the NS#1 and groups 1..3 in the NS#2 inside a single RE.
                  And, when retrieving the groups, we supply the namespace id.

                  Is it close to what you need?

                   
                  • saktheesh p

                    saktheesh p - 2003-08-26

                    yes u were almost  near

                    i have come up with another problem of same sort ( if i can solve this ill not have that problem )
                    is there any way to find out the index in the reguar expression that nmatched the text like
                    aabc|nngha|jjtk is the reguar expression and "nngha" is my matched text is there any way to find that nngha matched from position 5 of the regular expression

                    thanks in advance

                     
    • Nobody/Anonymous

      I think saktheesh means more like:

      ...
      MatchResult match = iterator.nextMatch();
      for (all groups) {
      int startIdx = match.start(i);
      }

      which gives you the starting position on the searched text for a particular group on a particular match.

        []s Gustavo

       
    • Nobody/Anonymous

      Or maybe saktheesh wants the opposite, which would be the position on the regex.  If that's what he/she wants, I've never heard of anything close to it...

      []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.