Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - how to validate a regular expression itself?

I need to validate user input that might be a regular expression for filtering fields in his/her own application, how can I check the validity of that regex?

like image 742
fabio Avatar asked Oct 13 '22 17:10

fabio


1 Answers

Pass it into a dummy call to preg_match(), and see if it returns false (indicating an error)?

Note:: Depending on where you intend this application to be run, I would question the decision to allow users to do this. You could be setting yourself up for a denial-of-service attack; all a malicious user needs to do is provide an incredibly long, complicated regex string, and your server will grind to a halt.

like image 123
Oliver Charlesworth Avatar answered Nov 03 '22 04:11

Oliver Charlesworth