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?
Asked
Active
Viewed 646 times
5
-
possible duplicate http://stackoverflow.com/questions/172303/is-there-a-regular-expression-to-detect-a-valid-regular-expression – red-X Jan 20 '11 at 12:55
1 Answers
4
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.

Oliver Charlesworth
- 267,707
- 33
- 569
- 680
-
i like your idea, i thought to implement that one but thought it was kinda bad practice, so decided to come here. thanks for the advice – fabio Jan 20 '11 at 13:16