Coyolero
Coyolero

Reputation: 2433

Regex -Check if string start with zero + space or zero alone

I have this regular expression to math:

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

Answers (2)

ΩmegaMan
ΩmegaMan

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

Rohit Jain
Rohit Jain

Reputation: 213233

  • First of all, there is no need to put 0 in a character class.
  • Secondly your regex will not match more than a single character after whitespace. As you don't have any quantifier on dot - . 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

Related Questions