PHP regular expression? =?! What is the meaning

Source: Internet
Author: User

Let's take a look at the syntax of regular expressions first.

We see that there are (? =a) and (?!) in regular expressions. A) to indicate whether we need to match something.

So we have the need to not match something when we can use (?!). a). Let's say we want to match strings that don't contain hello.

^(?!. *hello)

here. * to indicate that there may be other characters before the Hello, why should I add ^, because if not added, it may match to the position after H.

We can now solve the problem of ABBA on the Regex golf. The problem is to match words that do not contain ABBA, such as Abba,anallagmatic should not match. So that's all you need to do with this problem.

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

And then use the mismatch, we can also solve the problem of prime, this problem is to let us match a few x strings, first look at the regular.

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

(xx+) is a match of 2 and more than 2 x, (xx+) \1+ is matching repeated occurrences of 2 or more strings, so (xx+) \1+ represents the strings of those not primes, then the prime string is to remove the number of primes, is the above regular expression.

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.