I am trying to allow an empty string or null string in my regular expression:
^[0-9][0-9]*
what should I add to achieve this, thanks
I am trying to allow an empty string or null string in my regular expression:
^[0-9][0-9]*
what should I add to achieve this, thanks
Probably, you just want:
^[0-9]*
As is, you have a non-optional character class which means no empty string will match.
You can use |
as 'or', so in this following regex, it is either empty, or [0-9]+
(|^[0-9][0-9]*)
Also, [0-9][0-9]*
can be simplified to [0-9]+
-- they are the same.
However, in this particular case, you can probably just do:
^[0-9]*
Are you trying to check to see if a string is empty? (as in only has whitespace) If so ^\w*$
would match such a string.
Also, you mentioned a null string. Depending on what language you're using, passing a NULL pointer to a regex function may result in a segfault.