0

I want make own pattern matcher.

I want allow like this characters only TN 08 AB 2233. How can I make a pattern for this?

I tried like below but its erase all characters what I type in edit text

^[A-Z]{1,2}[0-9]{4,5}[A-Z]{7,8}[0-9]{10,13}$

Thanks.

SpongePablo
  • 870
  • 10
  • 24
Razul
  • 99
  • 1
  • 12

2 Answers2

0

Try this,

 ^[A-Z]{2}[ -][0-9]{1,2}(?: [A-Z])?(?: [A-Z]*)? [0-9]{4}$

Hope this will work for you.

Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
0

what you are looking for is something like this ^[A-Z]{2}\\s\\d{2}\\s[A-Z]{2}\\s\\d{4}$

for explanation, the values in the curly brackets are not idices, but number of values.

so, [A-Z]{1,2} means a value between A-Z at least ond and max two times. [0-9]{4,5} means a value between 0 an 9 at least 4 and max 5 times. also you are missing the white spaces in your regex, if they are mandatory. \s defines any white space, the second \ is to escape the other \. if the are not mandatory, you can ignore them by a ? like this ^[A-Z]{2}(\\s)?\\d{2}(\\s)?[A-Z]{2}(\\s)?\\d{4}$. so they can be there, but do not have to be.

Rich
  • 1,015
  • 1
  • 10
  • 22