1. Matcher.group() throws IllegalStateException - If no match has yet been attempted, or if the previous match operation failed.
In your case you haven't attempted to match before calling group(). So the exception.
2. If attempted match fails group() still throws IllegalStateExc...
Hope the following clarifies:
package com.kash.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String s = "ftp://anon:bah@111.222.333.444:9999/path1/path2/";
String pattern = "^ftp://(/S+):(/S+)@(/S+):(/d+)(/S+)$";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(s);
/*
* ..call m.matches() or m.lookingAt() or m.find() here..
*/
if (m.matches()) {
for (int i = 0; i < m.groupCount(); i++)
System.out.println("Group" + i + ": " + m.group(i));
} else {
System.out.println("\"" + s + "\" did not match \"" + pattern + "\"");
System.out.println("so if I call m.group() it'll throw IllegalStateException !");
}
}
}
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
Offline 804 posts
since Feb 2007