Like I said in the title, I am having a hard time writing this particular regular expression.
Any help would be much appreciated.
My best attempt in trying to solve this problem was writing this:
^([0-9]*[1-9][0-9]*)*[02468]$
But this regular expression can’t recognize a number like "002". Then I have tried this:
^([0-9]*[1-9][0-9]*)*[02468]*[02468]$
Now regular expression recognizes "002", but also recognizes "000" (only zeros) and I can’t avoid that happening.
In conclusion, I am failing again and again in writing a regular expression that can recognize positive even numbers (including leading zeros, but excluding "000" – numbers with only zeros) like:
2, 001234, 00123400, 002, 20, 16 etc.
Thank you in advance.
>Solution :
You could use a positive lookahead for any digit except 0:
^(?=.*[1-9])[0-9]*[02468]$
(?=.*[1-9])– positive lookahead for any digit except0[0-9]*– any digit repeated 0 or more times[02468]– any even digit