0

I have this regex [a-zA-Z]\d\d\\$\d\d-\d\d\d\d and my value is A11$12-9190. But it is not matching. Any clues?

3
  • That regex is not valid. Commented Oct 27, 2011 at 16:00
  • What's the actual Java string you're using? Commented Oct 27, 2011 at 16:01
  • You are escaping \ before the $ thus reaching the end of the string yet you are searching for more digits afterwards. Not possible. Commented Oct 27, 2011 at 16:03

4 Answers 4

4

Each backslash \ have to be escaped by an extra backslash, \\, in java patterns. Note that your backslash in front of the dollar sign is already escaped.

The resulting regex would be:

[a-zA-Z]\\d\\d\\$\\d\\d-\\d\\d\\d\\d
Sign up to request clarification or add additional context in comments.

Comments

2

Basically what's happening is that Java assumes your \d is an escape character for the String, you'll need to transform it into \\d - the first \ to escape the second \ that escapes the d for the regex!

Comments

1

Your regex is not valid. e.g.,

  String test = "A11$12-9190";
  String regex = "[a-zA-Z]\\d\\d\\$\\d\\d-\\d\\d\\d\\d";

  System.out.println(test.matches(regex));

Comments

0

Are you escaping all the \? If you have a string, you'll have to do "\\d".

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.