943,083 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1711
  • Java RSS
Jan 6th, 2010
0

regex to parse input string

Expand Post »
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.

java Syntax (Toggle Plain Text)
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class test {
  5.  
  6. public static void main(String[] args) {
  7. String s = "ftp://anon:bah@111.222.333.444:9999/path1/path2/";
  8. Pattern p = Pattern.compile("^ftp://(/S+):(/S+)@(/S+):(/d+)(/S+)$");
  9. Matcher m = p.matcher(s);
  10.  
  11. for (int i=0; i<m.groupCount(); i++)
  12. System.out.println("Group"+i+": "+m.group(i));
  13.  
  14. }
  15. }

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

Help would be appreciated. Thnx in adv.
Last edited by TheWhite; Jan 6th, 2010 at 8:54 pm.
Reputation Points: 72
Solved Threads: 6
Junior Poster
TheWhite is offline Offline
174 posts
since May 2008
Jan 7th, 2010
0
Re: regex to parse input string
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:
Java Syntax (Toggle Plain Text)
  1. package com.kash.test;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9. String s = "ftp://anon:bah@111.222.333.444:9999/path1/path2/";
  10. String pattern = "^ftp://(/S+):(/S+)@(/S+):(/d+)(/S+)$";
  11. Pattern p = Pattern.compile(pattern);
  12. Matcher m = p.matcher(s);
  13.  
  14. /*
  15. * ..call m.matches() or m.lookingAt() or m.find() here..
  16. */
  17.  
  18. if (m.matches()) {
  19. for (int i = 0; i < m.groupCount(); i++)
  20. System.out.println("Group" + i + ": " + m.group(i));
  21. } else {
  22. System.out.println("\"" + s + "\" did not match \"" + pattern + "\"");
  23. System.out.println("so if I call m.group() it'll throw IllegalStateException !");
  24. }
  25. }
  26.  
  27. }
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Jan 7th, 2010
0
Re: regex to parse input string
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?
Reputation Points: 72
Solved Threads: 6
Junior Poster
TheWhite is offline Offline
174 posts
since May 2008
Jan 7th, 2010
0
Re: regex to parse input string
java Syntax (Toggle Plain Text)
  1. package com.kash.test;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9. String s = "ftp://anon:bah@111.222.333.444:9999/path1/path2/";
  10. String pattern = "^ftp://(\\S+):(\\S+)@(\\S+):(\\d+)(\\S+)$";
  11. Pattern p = Pattern.compile(pattern);
  12. Matcher m = p.matcher(s);
  13.  
  14. /*
  15.   * ------------THIS COMMENT IS STILL VALID------------
  16. * ..call m.matches() or m.lookingAt() or m.find() here..
  17. */
  18.  
  19. if (m.matches()) {
  20. for (int i = 0; i < m.groupCount(); i++)
  21. System.out.println("Group" + i + ": " + m.group(i));
  22. } else {
  23. System.out.println("\"" + s + "\" did not match \"" + pattern + "\"");
  24. System.out.println("so if I call m.group() it'll throw IllegalStateException !");
  25. }
  26. }
  27.  
  28. }

Output produced:
Java Syntax (Toggle Plain Text)
  1. Group0: ftp://anon:bah@111.222.333.444:9999/path1/path2/
  2. Group1: anon
  3. Group2: bah
  4. Group3: 111.222.333.444
  5. Group4: 9999
Last edited by thekashyap; Jan 7th, 2010 at 6:12 am. Reason: added full code
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Jan 7th, 2010
0
Re: regex to parse input string
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?
Reputation Points: 72
Solved Threads: 6
Junior Poster
TheWhite is offline Offline
174 posts
since May 2008
Jan 7th, 2010
0
Re: regex to parse input string
It turns out m.groupCount() does not include group 0, so the for loop needs to be bound by m.groupCount() + 1.

Thanks everyone.
Reputation Points: 72
Solved Threads: 6
Junior Poster
TheWhite is offline Offline
174 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: loading images...
Next Thread in Java Forum Timeline: applet





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC