Danith Kumarasinghe
Danith Kumarasinghe

Reputation: 201

java regex for validate id

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".

Upvotes: 2

Views: 1206

Answers (1)

Michael Markidis
Michael Markidis

Reputation: 4191

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

Upvotes: 3

Related Questions