How to clean up results in Matcher Class?

View: New views
3 Messages — Rating Filter:   Alert me  

How to clean up results in Matcher Class?

by Harsha1 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
I have to clear the result from the Matcher class which gets generated after =~
I have code like this...

                int i = 0
                def sent = 'Hall Of Fame'
                if(sent =~ "([^\\s]+\\s[^\\s]+\\s[^\\s]+)")
                {
                       /* HERE I NEED TO CLEAR THE RESULT, Which is thr in Matcher class */
                       
                        sent =~ "([^\\s]+)\\s([^\\s]+)\\s([^\\s]+)"
                       
                        println Matcher.lastMatcher[0][1]
                        println Matcher.lastMatcher[0][2]
                        println Matcher.lastMatcher[0][3]
                }

Re: How to clean up results in Matcher Class?

by Ted Naleid-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You shouldn't need to clean up results (or use an if statement like that).  Use the "find" method (on groovy 1.6.1 and above):

def sent = "Hall of Fame"

sent.find(/(\S+)\s(\S+)\s(\S+)/) { full, first, second, third ->
   println first
   println second
   println third
}

prints:

Hall
of
Fame

On Thu, Jun 25, 2009 at 7:14 AM, Harsha1 <99harsha.h.n99@...> wrote:

Hi,
I have to clear the result from the Matcher class which gets generated after
=~
I have code like this...

               int i = 0
               def sent = 'Hall Of Fame'
               if(sent =~ "([^\\s]+\\s[^\\s]+\\s[^\\s]+)")
               {
                      /* HERE I NEED TO CLEAR THE RESULT, Which is thr in
Matcher class */

                       sent =~ "([^\\s]+)\\s([^\\s]+)\\s([^\\s]+)"

                       println Matcher.lastMatcher[0][1]
                       println Matcher.lastMatcher[0][2]
                       println Matcher.lastMatcher[0][3]
               }
--
View this message in context: http://www.nabble.com/How-to-clean-up-results-in-Matcher-Class--tp24202110p24202110.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




Re: How to clean up results in Matcher Class?

by edmund zheng :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It is strange that you use lastMatcher to access the result of match. I would you like these statements:
 
m=sent =~ "([^\\s]+)\\s([^\\s]+)\\s([^\\s]+)"
println m[0][0]
println m[0][1]
 
Anyway, your program can still work only you replace sent =~ "([^\\s]+)\\s([^\\s]+)\\s([^\\s]+)" with
(sent =~ "([^\\s]+)\\s([^\\s]+)\\s([^\\s]+)") as Boolean
 
       def sent = 'Hall Of Fame'
       if(sent =~ "([^\\s]+\\s[^\\s]+\\s[^\\s]+)")
       {
          println "hello"
          /* HERE I NEED TO CLEAR THE RESULT, Which is thr in Matcher class */
          (sent =~ "([^\\s]+)\\s([^\\s]+)\\s([^\\s]+)") as Boolean
          println Matcher.lastMatcher[0][0]
          println Matcher.lastMatcher[0][1]
          println Matcher.lastMatcher[0][2]
          println Matcher.lastMatcher[0][3]
       }
   
The reason is the lastMatcher is not saved when it is defined. It is saved when it is casted to Boolean and other a couple of situations. I don't know why.

On Thu, Jun 25, 2009 at 8:14 PM, Harsha1 <99harsha.h.n99@...> wrote:

Hi,
I have to clear the result from the Matcher class which gets generated after
=~
I have code like this...

               int i = 0
               def sent = 'Hall Of Fame'
               if(sent =~ "([^\\s]+\\s[^\\s]+\\s[^\\s]+)")
               {
                      /* HERE I NEED TO CLEAR THE RESULT, Which is thr in
Matcher class */

                       sent =~ "([^\\s]+)\\s([^\\s]+)\\s([^\\s]+)"

                       println Matcher.lastMatcher[0][1]
                       println Matcher.lastMatcher[0][2]
                       println Matcher.lastMatcher[0][3]
               }
--
View this message in context: http://www.nabble.com/How-to-clean-up-results-in-Matcher-Class--tp24202110p24202110.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email