Regular expressions in which PHP matches consecutive numbers or letters

Source: Internet
Author: User
Regular expression rules: "/Rules need to be written in the middle of 2 slashes/".

(.: decimal point) is used to match all characters except the line break.

(\s: Backslash lowercase s) is used to match a single space character, including the TAB key and line break;

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

(\d: Backslash D) is used to match numbers from 0 to 9, or it can be written like this: [0-9]

(\w: Backslash lowercase w) used to match letters, numbers, or underscore characters;

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

Meta characters include: +, *,?

Meta-characters are confusing to understand, so I did the code results later

The "+" metacharacters stipulate that their leading characters must appear consecutively or multiple times = For example,/es+/matches the "Tesseessssseast12354haeasashaha" string, first to match the first letter e, and then to match the s,s must appear one or more times, see the instance. The "*" meta-character specifies that its leading character must appear 0 or more times = For example,/es*/matches the "Tesseessssseast12354haeasashaha" string, first with the first letter E, the following s appearing 0 times, or in succession, to see the instance. “?” Metacharacters stipulate that their leading objects must appear consecutively 0 times or once = For example/es?/matches the "Tesseessssseast12354haeasashaha" string, first with the first letter E, The following s appear 0 times or at most once (that is, the last letter S does not recur).

Example 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:

Also if you feel the meta-character "+*?" "It is difficult to understand that this {} method can be used instead:

For example es* we can write es{0,}, and es+ we can write es{1,},es? can be written as es{0,1}, note: Do not write (and no spaces) when you are unsure.

Of course we have to specify how many times it can be written like this: es{3} means S appears 3 times

Extrapolate

For example, to replace a continuous number of spaces for a single space, I can write: Preg_replace ("/\s+/", "", $str);
For example, to find the number in the string (integer): Preg_replace ("/\d+/", "(\\0)", $str); \\0 is a string value that represents a conforming rule

For example, to find the number with a decimal point in the string: Preg_replace ("/\d+\.\d+/", "(\\0)", $str); Here's the "\." Indicates the output decimal point

Find a string consisting of letters: Preg_replace ("/[a-za-z]+/", "(\\0)", $STR)

Find a string consisting of (Letters, numbers mixed): Preg_replace ("/([a-za-z]|\d) +/", "(\\0)", $STR)

The "or" operation in the regular expression, using the "|"

For example, the above example: Find a string ([a-za-z]|\d) consisting of a mixture of (letters or numbers) ([a-za-z]|[ 0-9])

"^" is considered a negation operator when it appears in [], and [^0-9] represents any character except a number.

When "^" is outside the "[]" or "[]", it should be treated as a locator.

A locator means, for example: "^the": The first must have "the" string, similar: "en$": the $ symbol must be the end of en.

In fact, you will find the regular expression is very simple, unless I wrote the tutorial really have a problem.

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.