Regular Expression strings do not contain matching techniques. Regular Expression strings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.