[Garrett-Smiths-Computer:~/Documents/java/patterns] garrett% javaPatterns 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 ]
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).