Matches Throwing IllegalStateException after Matches

I have some strange problems with matches, hoping that someone can shed some light.
According to Java docs:

public boolean matches () ... If the match ends, then more information can be obtained through the beginning, end, and group methods.

Some codes:

private static Hashtable<String,String> splitAddress(String address){ Hashtable<String,String> result = new Hashtable<String,String>(); Matcher m = addrLong.matcher(address); if (m.matches()) { result.put("number", m.group(1)); 

Here it produces:

 java.lang.IllegalStateException: No match found java.util.regex.Matcher.group(Matcher.java:485) splitAddress(WebServiceHelper.java:699) 

This one is strange to me. Here is some more info if this helps:

  private static final String numberRegex = "[0-9]*[a-zA-Z]?"; // 123a 123 private static final String compassRegex = "N|E|S|W|NORTH|EAST|SOUTH|WEST|NORD|EST|SUD|OUEST"; private static final String typeRegex = "STREET|ST|DRIVE|DR|AVENUE|AVE|AV|ROAD|RD|LOOP|LP|COURT|CT|CIRCLE|LANE|LN|BOULEVARD|BLVD|CRESCENT|CR"; addrLong = Pattern.compile("(" + numberRegex + ")\\s(.*)\\s(" + typeRegex + ")\\s?(" + compassRegex + ")?"); 

The input string I tested is "12 CLARE ST E"

Thanks!

Edit: Sorry, I inserted my addrShort declaration instead of my addrLong

Edit2: I know that naming conventions are being violated. I did not write this part, I swear.

UPDATE:

Successfully executed as a separate function.

Any ideas why it will break in tomcat environment?

I will see if I can find anything that can affect this, but my addrLong is my only static variable and is not used anywhere else.

It drives me crazy. I even tried:

  Pattern p = Pattern.compile("(" + numberRegex + ")\\s(.*)"); Matcher m = p.matcher(address); if (m.matches()) { result.put("number", m.group(1)); 

in my server environment and it does not work.

UPDATE 2

It even works great when it is alone in the servlet. I'm at a dead end. Any hints or ideas are greatly appreciated.

UPDATE 3

Screw it, I just move the function to another class. Thanks for your help @ mjg123, you have a well-deserved check mark.

It will bother me forever ...

+7
source share
1 answer

I copied and pasted your code, and it compiles and runs as expected, with no exception. Is there any other part of your code causing this?


My full code is:

 public class StackOverflow { private static final String numberRegex = "[0-9]*[a-zA-Z]?"; // 123a 123 private static final String compassRegex = "N|E|S|W|NORTH|EAST|SOUTH|WEST|NORD|EST|SUD|OUEST"; private static final String typeRegex = "STREET|ST|DRIVE|DR|AVENUE|AVE|AV|ROAD|RD|LOOP|LP|COURT|CT|CIRCLE|LANE|LN|BOULEVARD|BLVD|CRESCENT|CR"; private static final Pattern addrLong = Pattern.compile("(" + numberRegex + ")\\s(.*)\\s(" + typeRegex + ")\\s?(" + compassRegex + ")?"); public static void main(final String[] args) { final String address = "12 CLARE ST E"; final Hashtable<String, String> result = splitAddress(address); System.out.println(result.get("number")); } private static Hashtable<String, String> splitAddress(final String address) { final Hashtable<String, String> result = new Hashtable<String, String>(); final Matcher m = addrLong.matcher(address); if (m.matches()) { result.put("number", m.group(1)); } return result; } } 

Which works fine and prints 12 as output.

+3
source

All Articles