2

I want validate ID using java regex.

  1. this ID two parts divided by '-' character.
  2. left part has fixed 2 capital English letters.
  3. right part has digits where length of digits between 2 and 5.

Here's the code I've tried:

boolean x=l.matches("(?i)[A-Z]{2}-\\[2-3]");

I used "HT-43" as input. I want to get answer as "YES" but I get "NO".

1
  • I don't understand why, since you begin your pattern with [A-Z]{2} (that is correct and make sense), you ends your pattern with \\[2-3] that doesn't make sense at all! You have missed something in the regex tutorial or something is missing in the question. Read it one more time, otherwise you will always ask this kind of strange questions. Commented May 29, 2017 at 23:07

1 Answer 1

3

You were close, try the following:

String num = "HT-43";

boolean x=num.matches("[A-Z]{2}-\\d{2,5}");

System.out.println(x);

Outputs:

true

Demo: https://regex101.com/r/UWGyB3/1

Sign up to request clarification or add additional context in comments.

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.