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"

Thanks!

Edit: Oops, I pasted my addrShort declaration instead of my addrLong

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

UPDATE:

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.

UPDATE 2

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

UPDATE 3

Screw it, I'm just moving the function out into another class. Thanks for your help @mjg123, have a well-deserved check mark.

This is going to bother me forever...


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.

链接地址: http://www.djcxy.com/p/5180.html

上一篇: 重用NSURLConnection的一个实例

下一篇: Matcher在匹配后抛出IllegalStateException