1

Is there a regexp to check if a string is a valid php regexp ?

In the back office of my application, the administrator define the data type and he can define a pattern as a regexp. For example /^[A-Z][a-zA-Z]+[a-z]$/ and in the front office, i use this pattern for validate user entries. In my code i use the PHP preg_match function

preg_match($pattern, $user_entries); 

the first argument must be a valid PHP regexp, how can i be sure that $pattern is a valid regexp since it a user entrie in my back office.

Any idea ?

2
  • A caveat about allowing execution of arbitrary regexes: If only admins can define regexes, and your admins know their way around regexes, then it should be fine. But be aware that the "right" combination of regex and data can lead to catastrophic backtracking, essentially blocking your application. For example, the regex .*.*.*.*.*.*.*^ will keep your regex engine busy for a long time on any string that's longer than a few characters (>590.000 iterations when applied to 1111111111111111, for example). Commented Jul 7, 2011 at 14:20
  • Even worse, using the /e modifier the replacement will be evaluated as PHP code if the regex is used with preg_replace. Commented Jul 7, 2011 at 14:51

4 Answers 4

2

Execute it and catch any warnings.

$track_errors = ini_get('track_errors');
ini_set('track_errors', 'on');
$php_errormsg = '';
@preg_match($regex, 'dummy');
$error = $php_errormsg;
ini_set('track_errors', $track_errors);
if($error) {
    // do something. $error contains the PHP warning thrown by the regex
}

If you just want to know if the regex fails or not you can simply use preg_match($regex, 'dummy') === false - that won't give you an error message though.

Sign up to request clarification or add additional context in comments.

Comments

0

As a work-around, you could just try and use the regex and see if an error occurs:

function is_regex($pattern)
{
  return @preg_match($pattern, "") !== false;
}

The function preg_match() returns false on error, and int when executing without error.

Background: I don't know if regular expressions themselves form a regular grammar, i.e. whether it's even possible in principle to verify a regex with a regex. The generic approach is to start parsing and checking if an error occurs, which is what the workaround does.

Comments

0

Technically, any expression can be a valid regular expression...

So, the validity of a regular expression will depend on the rules you want to respect.

I would:

  1. Identify the rules your regex must do
  2. Use a preg_match of your own, or some combination of substr to validate the pattern

Comments

0

You could use T-Regx library:

<?php
if (pattern('invalid {{')->valid()) {

https://t-regx.com/docs/is-valid

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.