I am having some difficulty with the following for a regular expression:
G followed by 1-5 numbers
or G followed by 4 numbers followed by a single letter A-Z
Could someone please assist?
e.g. of valid entries would be:
G2
G12
G123
G1234
G12345
G1234A
Thanks
^[G][0-9]{1,5}?$|^[G][0-9]{4}[A-Z]?$
^[G]
means starts with G
[0-9]{1,5}
means next 1 to 5 letters are numeric
[0-9]{4}
means next 4 letters are numeric
[A-Z]
means last character must be a letter between A -Z.
Try this Regex
^\b[G][0-9]{1,5}?$|^[G][0-9]{4}[A-Z]?$
OP:
Regex Explanation
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
[G] any character of: 'G'
--------------------------------------------------------------------------------
[0-9]{1,5}? any character of: '0' to '9' (between 1
and 5 times (matching the least amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
[G] any character of: 'G'
--------------------------------------------------------------------------------
[0-9]{4} any character of: '0' to '9' (4 times)
--------------------------------------------------------------------------------
[A-Z]? any character of: 'A' to 'Z' (optional
(matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
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