I was trying to find a regex that matches any string! and after some search I found almost all the answers says that [\s\S]
will match any string as said here or .*
as said here
But while playing a bit with PHP preg_match
I found that an empty regex is matching any string!
if(preg_match("//u", "")) echo "empty string matchs\n";
else echo "empty string does not match\n";
if(preg_match("//u", "abc")) echo "abc matchs\n";
else echo "abc does not match\n";
if(preg_match("//u", "\n")) echo "new line matchs\n";
else echo "new line does not match\n";
if(preg_match("//u", "/")) echo "/ matchs\n";
else echo "/ does not match\n";
exit;
this will output
empty string matchs
abc matchs
new line matchs
/ matchs
live demo (https://eval.in/845001)
Can I use this empty regex safely to match anything ? and what does an empty regex mean ?
If you are asking why would I need a regex that matches anything, that is because I'm using a function that requires a regex parameter as part of it's string validation functionality and I want it to accept anything.