I would like to capture a string that meets the criteria:
may be empty
if it is not empty it must have up to three digits (-> \d{1,3})
[A-Z]?)/) (-> \/?); if it is followed by a forward slash it must have from one to three digits
(-> \d{1,3})Here's a valid input:
Here's invalid input:
I've come up with the following ^\d{0,3}[A-Z]{0,1}/?[1,3]?$ that satisfies conditions 1-3. How do I deal with 4 condition? My Regex fails at two occassions:
77A/777/You may use
/^(?:\d{1,3}[A-Z]?(?:\/\d{1,3})?)?$/
See the regex demo
Details
^ - start of string(?:\d{1,3}[A-Z]?(?:\/\d{1,3})?)? - an optional non-capturing group:
\d{1,3} - one to three digits[A-Z]? - an optional uppercase ASCII letter(?:\/\d{1,3})? - an optional non-capturing group:
\/ - a / char\d{1,3} - 1 to 3 digits$ - end of string.Visual graph (generated here):

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With