Reputation: 2433
I have this regular expression to math:
String is a zero
"0 fkvjdm" // Must Match
"0" // Must match
"0.56" // NOT match
Here is the regular expression I'm using:
^([0]$|([0]\s+.))
Is there a way to improve it? or, is it has a bug?
Thanks a lot for your help.
Environment
Upvotes: 2
Views: 3192
Reputation: 31616
Seems like the second character is what causes the match to fail. If the second character is a period, then don't match; otherwise match. ?!
says if what it matches succeeds, fail the whole match. Hence if the second character is a period, it will fail.
^0(?!\.).*
Upvotes: 1
Reputation: 213233
0
in a character class. .
in 2nd part of your regex. To match more characters after whitespace, you should use .*
(0 or more) or .+
(1 or more).To improve in clarity, you can make use of optional quantifier here:
^0(\s+.*)?$
Upvotes: 5