Home > Blockchain >  regex for n characters or at least z characters
regex for n characters or at least z characters

Time:03-02

I have a input control (.net Regex and must be 1 liner) that I have to make sure it's either 15 characters long with - . or input box can have N/A

for example, 1443-RXSW3-8738 - this is 15 characters long or N/A

So far I have this ^[a-zA-Z0-9]{15}$‍‍ and this only makes sure it has at least 15 character.

Is it possible to have a 1 line Regex for this? The following works. I just have to make sure if it's 15 characters long then it must have hyphen.

^[a-zA-Z0-9]{15}$‍‍|(N\/A\b)

Input box must have 15 characters long and must have -. or it can have N

CodePudding user response:

Use ^([a-zA-Z0-9-]{15}|N/A)$. We simply add hyphen to the character class at either the end or the start to ensure it is treated as a normal character.

Try it out at http://regexstorm.net/tester?p=^([a-zA-Z0-9-]{15}|N/A)$&i=1443-RXSW3-8738

Reference: https://docs.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions#positive-character-group--

  • Related