I have this regex I am supposed to create that accepts the following formats:
This is what I have come up with so far: ^([a-zA-Z]([a-zA-Z]|-)[a-zA-Z])|[0-9]{4}$
The issue with this regex is that it accepts a combination of 4 letters as well, but I only want three consecutive letters. What do I need to change in my regex to accomplish this?
You may use:
^(?:[a-zA-Z][a-zA-Z-][a-zA-Z]|[0-9]{4})$
RegEx Demo
RegEx Details:
^: Start(?:: Start non-capture group that match either of these 2 alternatives
[a-zA-Z][a-zA-Z-][a-zA-Z]: Match 3 letters or a letter-letter|: OR[0-9]{4}: Match 4 digits): End non-capture group$: EndIf 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