• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

matcher.matches()

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain this?

[Garrett-Smiths-Computer:~/Documents/java/patterns] garrett% java Patterns
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:353)
at Patterns.main(Patterns.java:54)


[Garrett-Smiths-Computer:~/Documents/java/patterns] garrett% java Patterns
1x
Unless I call m.matches, the pattern fails. Why? Is this a bug?
[ January 18, 2003: Message edited by: Garrett Smith ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, this is normal behavior. A Matcher does not automatically try to match a given pattern until you call either matches() or find() - because it does not know in advance whether you want it to match the entire input at once (as for matches()) or just look for a match somewhere within the input (as for find()). Thus, you must call either matches() or find() (and check that the return value is true) before trying to use group(int).
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Since no previous match exists, then the IllegalStateException is thrown.
matches() tries to match a pattern, thereby changing the state of the matcher.
reply
    Bookmark Topic Watch Topic
  • New Topic