Can missing delimiter errors in a preg/PHP regexp be read programmatically?

Asked
Active3 hr before
Viewed126 times

6 Answers

missingerrors
90%

I would ask the user to enter the regex only (no delimiters, separate input fields for flags) then combine them into the code and produce the final string to feed to preg_match() and friends. Something like: '/'.preg_quote($_GET['regex']).'/'.$modifiers. Check regex101.com for inspiration. – axiac May 2 '15 at 19:12 ,I have a need to detect whether a regular expression is valid, so that invalid ones can be gracefully rejected in a user interface. On Stack Overflow there is a clever abomination to do this with another regular expression, which I plan to strenuously avoid.,There is a much simpler approach of running a match and checking for errors, which returns the correct boolean result, but it would be interesting to get the failure reason/message as well:, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

Here I just turn on output buffering with ob_start(). Then I capture the last error with error_get_last(). Then I end the output buffering with ob_end_clean(). After this you can check if the array is empty and if not an error occurred.

ob_start();
$result = preg_match(".*", "Location: xy");
$error = error_get_last();
ob_end_clean();

if(!empty($error))
echo "<b>Error:</b> " . $error["message"];
else
echo "no error found!";

output:

Error: preg_match(): No ending delimiter '.'
found

Then you can put your code into a try - catch block and catch the exception.

set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
    // error was suppressed with the @-operator
    if (0 === error_reporting()) {
        return false;
    }
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});

try {
    $result = preg_match(".*", "Location: xy");
} catch(Exception $e) {
    echo "<b>Error:</b> " . $e->getMessage();
}
load more v
88%

I need to determine if a regex is valid so that invalid ones can gracefully be rejected in the UI. Stack overflow is a clever abomination to do this with another regex that I plan to strenuously avoid.,There is a much simpler approach of doing match and error checking that returns the correct boolean result, but it would be interesting to get the reason for the failure / message as well:,PHP Warning: preg_match (): delimiter must not be alphanumeric or backslash in ... on line ...,What is the difference between an activity intent filter and a broadcast receiver? - android

There is a much simpler approach of doing match and error checking that returns the correct boolean result, but it would be interesting to get the reason for the failure / message as well:

// The error is that preg delimiters are missing
$testRegex = 'Location: (.+)';

// This bit is fine
$result = preg_match($testRegex, ''); // returns false i.e. failure
$valid = is_int($result); // false, i.e. the regex is invalid

// Returns PREG_NO_ERROR, which means no error occured
echo preg_last_error().
"\n";

(adsbygoogle = window.adsbygoogle || []).push({});
load more v
72%

When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character. Leading whitespace before a valid delimiter is silently ignored. , You may add pattern modifiers after the ending delimiter. The following is an example of case-insensitive matching: , It is also possible to use bracket style delimiters where the opening and closing brackets are the starting and ending delimiter, respectively. (), {}, [] and <> are all valid bracket style delimiter pairs. , If the delimiter needs to be matched inside the pattern it must be escaped using a backslash. If the delimiter appears often inside the pattern, it is a good idea to choose another delimiter in order to increase readability.

/foo bar/
# ^ [ ^ 0 - 9] $ # +
   php +
   %
   [a - zA - Z0 - 9_ - ] %
load more v
65%

if (!preg_match('/^\[a-zA-Z]+$/', $input)) {
   // String contains not allowed characters ...
}
75%

The preg_match() function will tell you whether a string contains matches of a pattern.,In PHP, regular expressions are strings composed of delimiters, a pattern and optional modifiers.,The preg_match_all() function will tell you how many matches were found for a pattern in a string.,The preg_replace() function will replace all of the matches of the pattern in a string with another string.

In the example above, / is the delimiter, w3schools is the pattern that is being searched for, and i is a modifier that makes the search case-insensitive.

/

In the example above, / is the delimiter, w3schools is the pattern that is being searched for, and i is a modifier that makes the search case-insensitive.

i
load more v
40%

How can I convert ereg expressions to preg in PHP? (missing delimiters),Unknown modifier '/' in …? what is it? (on using preg_quote()),When your regular expression is missing delimiters. , When your regular expression is missing delimiters.

This is the code on line 38:

<?php echo str_replace("</ul></div>", "", preg_replace("<div[^>]*><ul[^>]*>", "", wp_nav_menu(array('theme_location' => 'nav', 'echo' => false)) )); ?>
load more v

Other "missing-errors" queries related to "Can missing delimiter errors in a preg/PHP regexp be read programmatically?"