PHP Reverse Preg_match

if(preg_match("/" . $filter . "/i", $node)) { echo $node; } 

This code filters the variable to decide whether to display it or not. An example entry for the $ filter would be "office" or "164 (. *) 976."

I would like to know if there is an easy way to say: if $ filter does not match in $ node. As a regular expression?

So ... not "if (! Preg_match", but more than $ filter = "! Office" or "! 164 (. *) 976", but one that works?

+4
source share
3 answers

This can be done if you definitely want to use a "negative regular expression" instead of simply inverting the result of a positive regular expression:

 if(preg_match("/^(?:(?!" . $filter . ").)*$/i", $node)) { echo $node; } 

will match the string if it does not contain regex / substring in $filter .

Explanation: (taking office as our sample line)

 ^ # Anchor the match at the start of the string (?: # Try to match the following: (?! # (unless it possible to match office # the text "office" at this point) ) # (end of negative lookahead), . # Any character )* # zero or more times $ # until the end of the string 
+10
source

Failure (?!...) is what you are looking for.

To exclude a specific line from anywhere in the object, you can use this double statement method:

 preg_match('/(?=^((?!not_this).)+$) (......)/xs', $string); 

This allows you to specify an arbitrary (......) basic regular expression. But you can just leave it if you only want to ban the line.

+6
source

Answer # 2 from mario is the correct answer, and here's why:

First, to respond to Justin Morgan's comment,

I am curious if you do not know what this is in contrast to the preg_match () approach? I am not in the place where I can check both of them. - Justin Morgan Apr 19 '11 at 21:53

Consider the shutter logic for a moment.

When to cancel preg_match (): when searching for a match and you want the condition to be 1) true for the absence of the required regular expression or 2) false for the presence of the regular expression.

When to use a negative statement in a regular expression: when searching for a match and you want the condition to be true if the string ONLY matches the regular expression and fails if anything else is found. This is necessary if you really need to test unwanted characters, allowing you to skip the allowed characters.

Negating the result (preg_match () === 1) only checks if the regular expression is present. If a bar is required and rooms are not allowed, the following will not work:

 if (preg_match('bar', 'foo2bar') === 1) { echo "found 'bar'"; // but a number is here, so fail. } if (!pregmatch('[0-9]', 'foobar') === 1) { echo "no numbers found"; // but didn't test for 'bar', so fail. } 

So, in order to really test a few regexes, a newbie would test with a few calls to preg_match () ... we know that this is a very amateurish way to do this.

So, Op wants to check the string for possible regular expressions, but the conditional expression can only be passed as true if the string contains at least one of them. For most simple cases, simply abandoning preg_match () is enough, but for more complex or advanced regex patterns, this will not. I will use my situation for a more realistic scenario:

Suppose you want to have a user form for a person’s name, in particular a surname. You want your system to accept all letters regardless of the case and location, accept hyphens, accept apostrophes, and exclude all other characters. We know that matching regular expressions for all unwanted characters is the first thing we think about, but imagine that you support UTF-8 ... it's a lot of characters! Your program will be almost as big as the UTF-8 table, on just one line! I don’t care what equipment you have, your server application has a finite limit on how long the command will be, not to mention the limit of 200 brackets in brackets, so the ENTIRE UTF-8 character table (minus [AZ], [az ], -, and ') is too long, no matter what the program itself will be HUGE!

Since we will not use if (! Preg_match ('. # \\ $ \% ... this can be quite long and impossible to evaluate ... in a line, to see if the line is bad, we should instead test more simple way, with a negative inverse expression of a statement in a regular expression, and then deny the overall result using:

 <?php $string = "O'Reilly-Finlay"; if (preg_match('/?![az\'-]/i', $string) === 0) { echo "the given string matched exclusively for regex pattern"; // should not work on error, since preg_match returns false, which is not an int (we tested for identity, not equality) } else { echo "the given string did not match exclusively to the regex pattern"; } ?> 

If we looked only for the regular expression [az \ '-] / i, all we say is a “matching string if it contains ANY of these things”, so bad characters are not checked. If we deny the function, we say "return false if we find a match containing any of these things." This is also not the case, so we need to say: "return false if we match ANYTHING in the regular expression", which is done using lookahead. I know that the bells go away in the head and they think about expanding the wildcards ... no, the lookahead does not do this, he just does the negation in every match and continues. Thus, it checks the first character for the regular expression, if it matches, it moves until it finds a mismatch or end. After its completion, everything that was found does not match the regular expression, is returned to the array of matches, or simply returns 1. In short, the assert negative in regex 'a' is the opposite of the regular expression 'b', where 'b' contains ALL compatible with 'a'. Great when the "b" will be painless.

Note: if there is an error in my regex, I apologize ... I have been using Lua for the last few months, so I can mix regex rules. Otherwise '?!' is the correct syntax for PHP.

0
source

All Articles