Home > database >  Regex expression help does not end in -0 or -00
Regex expression help does not end in -0 or -00

Time:09-22

I am trying to match a regex expression in a #####-## format where # is any digit from 0-9.

The -## suffix can be a single or double digit such as -01, -12, -1, -2, -9,etc. except -00 and -0.

So far I have:

"^[0-9]{6}(-(?!00)[0-9]{1,2})$"

Which works great for everything except something like 123456-0

How to add that exclusion?

CodePudding user response:

You can make the first zero optional and assert the end of the string in the assertion (?!0?0$)

^[0-9]{6}(-(?!0?0$)[0-9]{1,2})$

See a regex demo.

If you don't need the extra capture group:

^[0-9]{6}-(?!0?0$)[0-9]{1,2}$

CodePudding user response:

Try (regex101):

^\d{6}-(?:[1-9]|[1-9]0|\d[1-9])$
  • Related