• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

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.
 
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic