2

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

5 Answers 5

3

Probably, you just want:

^[0-9]*

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

Sign up to request clarification or add additional context in comments.

Comments

3

Get rid of the first digit:

^[0-9]*

Comments

0

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]*

Comments

0

this?:

^[\s\d]*

Match : "123" abc in "123 abc"

Comments

0

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.