Nikhil G
Nikhil G

Reputation: 1594

Regex for matching special alphanumeric patterns with zeros

I have created a regex to match if a string contains all 'zeroes.' 'All-zeroes' are not allowed in the input box. I have been able to achieve this requirement, however it is a hard-coded one.

The regex I have written is as follows:

(^[a-zA-Z](?!(0000000|000000\b|00000\b|0000\b|000\b|00\b|0\b))[0-9]{1,7}$)

How do I solve this problem?

Upvotes: 0

Views: 26

Answers (2)

Emma
Emma

Reputation: 27723

You could also use groups () with | and create match for your pattern, maybe similar to this RegEx:

^[A-Za-z](([1-9]+)|(0+[1-9]))$

enter image description here

And you might bound it with other boundaries, such as the number of chars for numbers or alpha.

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 370709

Assuming your current pattern is achieving what you want, you can simplify it by using negative lookahead for 0+$, ensuring that the first character is not followed by only zeros:

^[a-zA-Z](?!0+$)[0-9]{1,7}$

https://regex101.com/r/neOfJm/1

Upvotes: 1

Related Questions