PHP matches regular expressions of consecutive numbers or letters

Source: Internet
Author: User
PHP matches regular expressions of consecutive numbers or letters. For more information, see. Regular expression writing rules: "/rules must be written in the middle of two slashes /".

(.: Decimal point) is used to match all characters except line breaks.

(\ S: lower-case backslash (s) is used to match a single space character, including the tab key and line break;

(\ S: uppercase backslash (S) is used to match all characters except a single space character;

(\ D: Backslash d) is used to match numbers from 0 to 9. It can also be written as follows: [0-9]

(\ W: lower-case backslash (w) is used to match letters, numbers, or underscores;

(\ W: uppercase backslash (W) is used to match all characters that do not match \ w;

Metacharacters include: + ,*,?

Metacharacters are easy to understand, so I made code results later.

The "+" metacharacter specifies that the leading character must appear one or more times consecutively = for example,/es +/matches the string "tesseessssseast12354haeasashaha". First, it must match the first letter "e, then, matching s and s must appear once or multiple times. please refer to the instance. The "*" metacharacter specifies that the leading character must appear zero or multiple times in a row = for example,/es */matches the "tesseessssseast12354haeasashaha" string, first matches the first letter e, the following s appears zero times or consecutively. check the instance. "?" Metacharacter specifies that the leading object must appear for zero consecutive times or Once = for example,/es? /Matches the "tesseessssseast12354haeasashaha" string. First, it matches with the first letter e, and the second after it appears zero or at most once (that is, the last letter s does not appear repeatedly ).

Sample code:

$str = "tesseessssseast12354haeasashaha"; echo "=====".$str." "; echo "/es+/:".preg_replace("/es+/","-\\0-",$str)." "; echo "/es*/:".preg_replace("/es*/","-\\0-",$str)." "; echo "/es?/:".preg_replace("/es?/","-\\0-",$str)." "; ?>

Execution result:

In fact, if you read it carefully, you will find that the regular expression is quite simple, unless it is a problem with the tutorial I wrote.

For more articles related to matching regular expressions of consecutive numbers or letters in PHP, refer to PHP!

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.