Regular Expression Preg_Quote Symbols Are Not Detected

Function preg_quote works incorrect?

preg_quote does not escape the regex's delimiter by default, because it can be any non-alphanumeric, non-backslash, non-whitespace character.

Set its second parameter ($delimiter) to also escape forward slashes:

$escaped_symbols = preg_quote($allow_symbols, '/');
$pattern = "/^[$escaped_symbols\p{L}\p{N}]+$/iu";

New line symbols are not detected by regex

Your text contains literal \n in it, not newlines.

The regexp \n matches a newline, not literal \n. To match \n you need to use the regexp \\n. Escaping the backslash just allows the backslash to be passed to the regexp parser. You need to double-escape it so that the regexp will match \n, or use a raw string.

rex = re.sub(r"(\\n)+, " ", reviews[3])

See What exactly is a "raw string regex" and how can you use it?

Use preg_quote but keep certain special regular expression characters

You can undo the undesired escapes.

$str = preg_quote($str, '/');
$str = str_replace(['\\*', '\\\\'], ['*', '\\'], $str);

This can be a function:

function preg_quote_except($str, $except, $delim = NULL) {
$str = preg_quote($str, $delim);
for ($i = 0; $i < strlen($except); $i++) {
$from[] = '\\' . $except[$i];
$to[] = $except[$i];
}
return str_replace($from, $to, $str);
}

Then you would call it like:

$str = preg_quote_except($str, '\\*', '/');

I moved the delimiter parameter to the end so it can be optional.

Working regex pattern doesn't work in PHP despite preg_quote

preg_quote escapes characters that are regular expression syntax characters. These include . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -. Try not using preg_quote.

$pattern = '%(?<=@address|.)singleline(?=[^\]\[]*\])%';  
$output = preg_replace($pattern, "", $output);

EDIT:
You might want to use preg_quote if you had content you wanted to include in your regex pattern which contained characters used in regex syntax. For example:

$input = "item 1 -- total cost: $5.00";
$pattern = "/total cost: " . preg_quote("$5.00") . "/";
// $pattern should now be "/total cost: \$5.00/"
$output = preg_replace($pattern, 'five dollars', $input);

In this case, you need to escape the $ because it is used in the regex syntax. To search for it, your regex should use \$ instead of $. Using preg_quote performs this alteration for you.

Regex not displaying well whe using PHP preg_quote

Call preg_quote() on the string you're adding before you add it into the regex:

$mystring = "banana";
$regex_str = "/^" . preg_quote($mystring, "/") . "\-[a-z0-9]\-[a-z0-9]$/";

PHP preg_match and preg_quote doesn't match dollar sign

\b matches a position between a \w (word) and a \W (non-word)

\w is the same as [a-zA-Z0-9_] so $ and space are both non-word, so if the 'naughty_word' starts or ends with a symbol your expression can't match

you have to use something like

preg_match('/\s*+'.preg_quote($word).'\s*+/i',$string_to_check)

RegEx match with special characters

If you don't need the regular expression features, you can use indexOf:

if (string1.toLowerCase().indexOf(stringtarget.toLowerCase()) >= 0) {
// It's in there
}

Otherwise (and I'm guessing you're using match for a reason!), you'll have to pre-process the stringtarget to escape any special characters.

For instance, this is a function I used in a recent project to do exactly that, which is inspired by a similar PHP function:

if (!RegExp.escape) {
(function() {
// This is drawn from http://phpjs.org/functions/preg_quote:491
RegExp.escape = RegExp_escape;
function RegExp_escape(str) {
return String(str).replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!<>\|\:])/g, '\\$1');
}
})();
}

And so:

var match = string1.match(RegExp.escape(stringtarget),"ig");

preg_match: can't find substring which has trailing special characters

You can just check if the characters around the search word are not word characters with look-arounds:

$find = "website scripting @";
$string = "PHP is the website scripting @ language of choice.";

if (preg_match("/(?<!\\w)" . preg_quote($find, '/') . "(?!\\w)/i", $string)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}

See IDEONE demo

Result: A match was found.

Note the double slash used with \w in (?<!\\w) and (?!\\w), as you have to escape regex special characters in interpolated strings.

The preg_quote function is necessary as the search word - from what I see - can have special characters, and some of them must be escaped if intended to be matched as literal characters.

UPDATE

There is a way to build a regex with smartly placed word boundaries around the keyword, but the performance will be worse compared with the approach above. Here is sample code:

$string =  "PHP is the website scripting @ language of choice.";

$find = "website scripting @";
$find = preg_quote($find);
if (preg_match('/\w$/u', $find)) { // Setting trailing word boundary
$find .= '\\b';
}
if (preg_match('/^\w/u', $find)) { // Setting leading word boundary
$find = '\\b' . $find;
}

if (preg_match("/" . $find . "/ui", $string)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}

See another IDEONE demo



Related Topics



Leave a reply



Submit