Preg Match Empty String

Viewed 47121

How to preg_match for an null (empty) string??

I need something like:

/([0-9]|[NULL STRING])/
5 Answers

With PHP's regex you can just say:

/(\d|)/

It may be usefull...

As it is not easy to match an empty string but it is generally possible to match anything that is not an empty string, consider to turn it around and not match the opposite (double negative).
In this example, everything that is not (^) a non-digit (\D) for zero or one time (?):

[^\D]?

(or for specific ascii: [^\x00-\x29\x3a-\xff]?, or unicode: [^\u0000-\u0029\u003a-\uffff]?)

Matches an "" (<empty string>), "0", "1" .. "9", but not a "", (<space>), "A" (non-digit) or "123" (any longer string).

Related