Regular Expression Matching techniques that do not contain certain strings

Source: Internet
Author: User
This article mainly introduces the regular expression matching techniques that do not contain certain strings, and details the reasons for doing so, if you want to find the text that does not contain a certain string, you can refer to it. the programmer can use it in a regular expression, ^ (hede) to filter the "hede" string, but 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:

^ ((?! 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:

This question is to match words that do not include abba, such as abba and anallagmatic.

Regular expression code:

^ (?!. * (.) (.) \ 2 \ 1)

Then, we can solve the prime problem by using non-matching. this question matches the string of several x numbers. let's first look at the regular expression.

^ (?! (Xx +) \ 1 + $)

(Xx +) is to match two or more x, (xx +) \ 1 + is to match two or more strings, so (xx +) \ 1 + indicates the strings with non-prime numbers, so the prime number is to remove these non-prime strings, that is, the above regular expression.

For more articles about regular expression matching techniques that do not contain certain strings, refer to the PHP Chinese website!

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.