Need regex - disable all zeros

I want to check the string for the following conditions:

  • Must be 6 characters long
  • Only the first character can be alphanumeric, the rest must be numeric
  • If the first digit is alpha, it must be closed
  • Not all zeros

I have the following regex that gets everything except a portion of all zeros. Is there a way to disable all zeros?

^[A-Z0-9][0-9]{5}$

This is the only way to do this to check the regex (and allow "000000"), but then specifically verify that it is not "000000"?

Thanks.

+4
source share
6 answers

I just have a negative look to disable all 0s:

 /^(?!0{6})[A-Z0-9][0-9]{5}$/ 
+16
source

I would do two passes. One with your first regular expression and one with a new regular expression that searches for all zeros.

+1
source

What if you checked all cases of zeros first, and then determined that all zeros do not apply your regular expression?

 if ( NOT ALL ZEROS) APPLY REGEX 
+1
source

I think it will be done. It checks not 000000 and your original regular expression.

 (?!0{6})^[A-Z0-9][0-9]{5}$ 
0
source

(?!000000)[A-Z0-9][0-9]{5} if the lookahead is ok.

0
source

You can use (?!0+$) negative lookahead to avoid matching a string containing only 1 or more zeros:

 /^(?!0+$)[A-Z0-9][0-9]{5}$/ ^^^^^^^ 

See the demo of regex . This approach allows you to simply copy / paste the lookahead after ^ and not worry about how many characters correspond to the consuming part.

More details

  • ^ - start of line
  • (?!0+$) - a negative result that does not match if there are 1 or more characters 0 to the end of the line ( $ )
  • [A-Z0-9] - ASCII uppercase letter or number
  • [0-9]{5} - five digits
  • $ is the end of the line.
0
source

Source: https://habr.com/ru/post/1400290/


All Articles