Regular Expression strings do not contain matching techniques. Regular Expression strings
We often encounter text that does not contain a string. What programmers can think of most easily is to use it in regular expressions,^(hede)To filter the "hede" string, but this method is incorrect. We can write as follows:[^hede]But such a regular expression completely means that the string cannot contain three characters, namely, 'h', 'E', and 'D. So what regular expressions can filter out information that does not contain the complete "hello" string?
In fact, regular expressions do not support reverse matching. Just like this problem, we can use a negative lookup to simulate reverse matching to solve our problem:
^((?!hede).)*$
The above expression can filter out information that does not contain the 'hede' string. As I mentioned above, this method is not a regular expression that is "good at", but it can be used in this way.
Explanation
A string consists of n characters. There is an empty character before and after each character. In this way, a string consisting of n characters has n + 1 null string. Let's take a look at the "ABhedeCD" string:
+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+ S = |e1| A |e2| B |e3| h |e4| e |e5| d |e6| e |e7| C |e8| D |e9| +--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+---+--+ index 0 1 2 3 4 5 6 7
All e numbers are empty characters. Expression(?!hede).Will look forward to see if there is no "hede" string in front. If there is no (other character), then.(Point number) will match these other characters. The "Search" of this regular expression is also called "zero-width-assertions" (zero-width assertions), because it does not capture any characters, just judgment.
In the preceding example, each empty character checks whether the character string prior to it is not 'hede'. If not.(Point number) is to match to capture this character. Expression(?!hede).So we wrap the expression in parentheses into groups and then use*(Asterisk) modifier-match 0 or multiple times:((?!hede).)*.
As you can understand, regular expressions((?!hede).)*Match string"ABhedeCD"The result is false becausee3Location,(?!hede)The match is invalid."hede"A string that contains the specified string.
In a regular expression,?!Whether to search forward in a definite way. It helps us solve the problem of "not included" string matching.
[Original English: Regular expression to match string not containing a word? ]
Article from: IT comments of external journals