tags:

views:

39

answers:

5

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

+1  A: 

Probably, you just want:

^[0-9]*

As is, you have a non-optional character class which means no empty string will match.

Matthew Flaschen
+1  A: 

Get rid of the first digit:

^[0-9]*
Avi
A: 

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]*
orangeoctopus
A: 

this?:

^[\s\d]*

Match : "123" abc in "123 abc"

Jet
A: 

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.

sigint
`\w` matches a word character, i.e., `[A-Za-z0-9_]`. The shorthand for whitespace is `\s`.
Alan Moore