PHP matches strings that start with "ABC" and Cannot End with "XYZ"

Source: Internet
Author: User
Tags php example php regular expression

This article introduces, with the PHP regular area with "ABC" beginning, and can not be "xyz" end of the string method, the need for a friend reference.

Requirements:
Use the PHP regular expression match to start with "ABC", but the end cannot contain the string "X", "Y", "Z".

Analytical:
ABC begins with a regular write: ^abc.
Start with ABC, followed by a string of strings
The general use of [^ ....] To deny it.
Since it is the negation of "X", "Y", "Z", it is [^xyz].

The complete regular expression is this:

^abc[^xyz]*$

The following is a full PHP example that uses this regular, as follows:

<?php$str = ' abcdef124f '; $search = '/^abc[^xyz]*$/'; if (Preg_match ($search, $str)) {echo $str. ' In accordance with <br/> ';} else {echo $str. ' does not conform to <br/> ';} Output abcdef124f conforms to $str = ' abcdef12x124 '; {preg_match ($search, $str) {echo $str. ' In accordance with <br/> ';} else {echo $str. ' does not conform to <br/> ';} Output abcdef12x124 does not conform to//edit by www.jbxue.com?>

PHP matches strings that start with "ABC" and Cannot End with "XYZ"

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.