I want to parse a string into something like an array:

ftp://user:pass@host:portpath
ftp://anon:1234@111.222.333.444:9999/path1/path2/

I read the java docs about using Pattern and Matcher but I keep getting an "IllegalStateException: No match found" error.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test {

	public static void main(String[] args) {
		String s = "ftp://anon:bah@111.222.333.444:9999/path1/path2/";
		Pattern p = Pattern.compile("^ftp://(/S+):(/S+)@(/S+):(/d+)(/S+)$");
		Matcher m = p.matcher(s);
		
		for (int i=0; i<m.groupCount(); i++)
			System.out.println("Group"+i+": "+m.group(i));

	}
}

"IllegalStateException: No match found" when it goes to print...

Help would be appreciated. Thnx in adv.

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 !");
		}
	}

}

I already know the reason no m.group() throws the exception is because no matches were found.

My problem is the pattern which I think should work against that string, but doesn't. Can someone help me figure out the correct pattern?

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);

		/*
                 * ------------THIS COMMENT IS STILL VALID------------
		 * ..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 !");
		}
	}

}

Output produced:

Group0: ftp://anon:bah@111.222.333.444:9999/path1/path2/
Group1: anon
Group2: bah
Group3: 111.222.333.444
Group4: 9999

ahhh I should have looked more carefully, slashes were wrong way :).


Also, its missing the last group which should be /path1/path2/ any suggestions on that?

It turns out m.groupCount() does not include group 0, so the for loop needs to be bound by m.groupCount() + 1.

Thanks everyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.