Regular Expression matching techniques that do not contain certain strings, regular expression strings

Source: Internet
Author: User

Regular Expression matching techniques that do not contain certain strings, regular expression strings

We often encounter text that does not contain a string. The programmer is most likely to think of using ^ (hede) in a regular expression to filter the "hede" string, however, this method is incorrect. We can write it like this: [^ hede], but such a regular expression completely means that the string cannot contain 'h', 'E ', 'D' contains three characters. 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:
Copy codeThe Code is as follows: ^ ((?! 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:

All e numbers are empty characters. Expression (?! Hede). It will look forward to see if there is no "hede" string in front. If there is no (another character), then. (point) 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 before it is not 'hede'. If not, this. (DOT) matches to capture the character. Expression (?! Hede). Execute only once. Therefore, we wrap this expression in parentheses into groups and then modify it with * (asterisk) -- match 0 or multiple times:Copy codeThe Code is as follows :((?! Hede ).)*.
You can understand that regular expressions ((?! Hede).) * The result of matching the string "ABhedeCD" is false because it is at the e3 position ,(?! The specified string is contained in the "hede" string.

In the regular expression ,?! Whether to search forward in a definite way. It helps us solve the problem of "not included" string matching.


How does a regular expression match a string but does not contain a specified string?

(?!. * BEA) ^. * Exception $

How does a regular expression match a string but does not contain a specified string?

If the entire sentence is used:
. * No. * Department. * name. * |. * Department. * name. * Rule .*

If a part of a sentence is used:
(? =. * No). * Department. * name. * |. * Department. * name .*(? = Rule .*)

For more precise matching, please "Ask.

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.