0

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());
    }
}
3
  • At the start of the pattern is []. Are you wanting to search for those literal characters? I.e. an open square bracket and a close square bracket? If so, you'll need to escape them: \\[\\]. Commented Jun 20, 2013 at 14:59
  • 2
    There are many ways to fix the pattern. But we don't know what you are trying to match, so any answer would be just a guess.
    – jlordo
    Commented Jun 20, 2013 at 15:00
  • I've voted to close as too localized. It's not clear what the original question was seeking, so I'm not sure the answer is useful for anyone other than the OP. Commented Jun 21, 2013 at 7:47

1 Answer 1

1

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}$");
2
  • 1
    That certainly removes the error. I guess the OP can confirm whether this is what was desired. For example, this would also remove the error: "^\\[\\]0-9*#X[^-]{1,50}$". Commented Jun 20, 2013 at 14:58
  • Thanks a lot guys, escaping the [ and ] I did not know they are considered as special characters. I am trying to match a phone number and I got the regex from Cisco data Dictionary (cisco.com/en/US/docs/voice_ip_comm/cucm/datadict/9_1_1/…) in case you are curious about what is it for.
    – laitha0
    Commented Jun 20, 2013 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.