PHP matches consecutive numbers or letters of regular expressions _ajax related

Source: Internet
Author: User
Tags lowercase

Regular expression rules:"/ rules need to be written in the middle of 2 slashes /".

  • ( . : decimal point) is used to match all characters except line breaks.
  • ( \s: Backslash lowercase s) is used to match a single spaces, including tab keys and line breaks;
  • ( \s: Backslash capital S) is used to match all characters except a single spaces;
  • ( \d: backslash D) is used to match numbers from 0 to 9; You can also write: [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 easily confusing to understand, so I did a screenshot of the code results behind

Copy Code code as follows:

The "+" metacharacters specify that their leading characters must occur continuously one or more times

= such as/es+/
Match the "Tesseessssseast12354haeasashaha" string, first with the first letter E, and then the matching s,s must appear one or more times, take a look at the instance screenshot.

The "*" Meta character stipulates that its leading character must appear 0 or more consecutive times

= such as/es*/
Matches the "Tesseessssseast12354haeasashaha" string, first with the first letter E, followed by 0 or successive occurrences, looking at the example.

“?” Metacharacters require that the leading object must appear 0 consecutive times or once

= such as/es?/
Matches the "Tesseessssseast12354haeasashaha" string, first with the first letter e, followed by a 0 or more occurrences (the last letter S does not recur).

Sample code:
Copy Code code as follows:

$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). "
";
?>

Screenshot of execution results:

In addition, if you feel the meta character "+*"? "It's hard to understand that you can use this {} method 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: When the number of uncertainty is not written (and no space).

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

Extrapolate

For example, to replace a contiguous number of spaces for a space , I can write this:preg_replace ("/\s+/", "", $str);
For example, to find the number (integer) in the string: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 "\." Indicates the output decimal point

Find a string composed of letters:preg_replace ("/[a-za-z]+/", "(\\0)", $str)

Find a string composed of (alphanumeric):preg_replace ("/[a-za-z]| \d)+/"," (\\0), $str)

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

For example, the above example: Find a string consisting of (a letter or a number) ([a-za-z]| \d ) can also be written like this ([a-za-z]| [0-9] )

"^" is treated as a negation operator when it appears inside [],and[^0-9] represents any character other than a number.

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

locator meaning is like: " ^the ": The beginning must have "the" string; similar: " en$ ": $ sign representation must be en end."

In fact, a careful look at it will find that the regular expression is quite simple, unless the tutorial I wrote is really problematic.

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.