0

Guys Please help me in creating a regular expression for a string starts with four capital alpha and followed by numeric. total length should be minimum 15 and max 30

e.g.

ABCD12322323231 , ABCD12322323231343 is a valid
123ABC, ABC12321, ABCD12, ABCD-12323123123123213213213 are invalid

I were trying with

(^([A-Z]){4}){15,30}$)
Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93

2 Answers2

2

You could use this I believe:

[A-Z]{4}\d{11,26}
demogorgon
  • 474
  • 4
  • 20
1
[A-Z]{4}[0-9]{11,26}

Explanation:

[A-Z] => Anything between A to Z
{4}   => Repeat 4 times
[0-9] => Anything between 0 to 9
{11,26} => Repeat 11 to 26 times

The numeric part length is limited to between 11 to 26 so that the total length is between 15 to 30.

[0-9] is preferred to \d because the latter matches all Unicode digits rather than just 0-9.

Davis
  • 85
  • 2
  • 8
  • What's the difference between unicode digits and normal digits? – Kamran Shahid Aug 09 '17 at 08:37
  • [0-9] matches only 0123456789 characters, while \d matches [0-9] and other digit characters, for example Eastern Arabic numerals ٠١٢٣٤٥٦٧٨٩ https://stackoverflow.com/a/6479605/7362939 – Davis Aug 09 '17 at 10:21