laitha0
laitha0

Reputation: 4336

Java Regex Unclosed character class (PatternSyntaxException)

I am having trouble compiling a regex. I cant find what the problem is with this expression as I got it from Cisco documentation and I don't understand why it does not work. I'm hoping that somebody could tell me what is wrong with it. This is what I am trying to do:

public void test(){
    try{
        pattern.compile("^[]0-9*#X[^-]{1,50}$");
        System.out.println("Syntax is ok");
    } catch (PatternSyntaxException e) {
        System.out.println(e.getDescription());
    }
}

Upvotes: 0

Views: 882

Answers (1)

BackSlash
BackSlash

Reputation: 22233

This:

^[]0-9*#X[^-]{1,50}$

Doesn't work, you have to replace []0-9 with [0-9]:

^[0-9]*#X[^-]{1,50}$

UPDATE

As Duncan Jones says, maybe you wanted to match [] at the beginning of the string. In this case, you regex has to become

^\[\]0-9*#X[^-]{1,50}$

So:

pattern.compile("^\\[\\]0-9*#X[^-]{1,50}$");

Upvotes: 1

Related Questions