uzrbin
uzrbin

Reputation: 93

Matcher throwing IllegalStateException after matches

I'm having some strange issues with Matches, hoping someone could shed some light.
According to Java docs:

public boolean matches() ... If the match succeeds then more information can be obtained via the start, end, and group methods.

Some code:

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

This is where it throws:

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

This alone is strange to me. Here's some more info if it 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've been testing against is "12 CLARE ST E"

I know naming conventions are being broken. I didn't write that part, I swear.

This executes successfully as a standalone function.

Any ideas why it would break in a Tomcat environment?

I'll see if I can find anything that could be impacting this, but my addrLong is my only static variable and is not used anywhere else.

This is driving 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 fails.

It even runs fine when it's alone in a servlet. I'm stumped. Any hints or ideas greatly appreciated.

Upvotes: 7

Views: 2883

Answers (1)

Matthew Gilliard
Matthew Gilliard

Reputation: 9498

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


My complete 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 runs fine and prints 12 as its output.

Upvotes: 3

Related Questions