Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preg Match Empty String

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

I need something like:

/([0-9]|[NULL STRING])/ 
like image 643
Denis Pshenov Avatar asked Nov 27 '09 16:11

Denis Pshenov


People also ask

What does preg_ match return?

The preg_match() function returns whether a match was found in a string.

Is preg match case-insensitive?

preg_match is case sensitive. A match. Add the letter "i" to the end of the pattern string to perform a case-insensitive match.

What does Preg_match mean in PHP?

preg_match() in PHP – this function is used to perform pattern matching in PHP on a string. It returns true if a match is found and false if a match is not found. preg_split() in PHP – this function is used to perform a pattern match on a string and then split the results into a numeric array.


1 Answers

You can use ^ to match beginning-of-line, and $ to match end-of-line, thus the regular expression ^$ would match an empty line/string.

Your specific example (a line/string containing a single digit or being empty) would easiest be achieved with ^[0-9]?$.

like image 51
Mikael Auno Avatar answered Oct 03 '22 18:10

Mikael Auno