954,176 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

regex to parse input string

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.

TheWhite
Junior Poster
174 posts since May 2008
Reputation Points: 72
Solved Threads: 6
 

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

}
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

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?

TheWhite
Junior Poster
174 posts since May 2008
Reputation Points: 72
Solved Threads: 6
 
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
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

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?

TheWhite
Junior Poster
174 posts since May 2008
Reputation Points: 72
Solved Threads: 6
 

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

Thanks everyone.

TheWhite
Junior Poster
174 posts since May 2008
Reputation Points: 72
Solved Threads: 6
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You