PHP matches regular expressions of consecutive numbers or letters

Source: Internet
Author: User

Rules for writing regular expressions:"/The rule 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: Backslash (uppercase) is used to match all characters except a single space character;
  • (\ D: Backslash d) is used to match numbers from 0 to 9. You can also write it as follows:[0-9]
  • (\ W: Lowercase backslash (w) is used to match letters, numbers, or underlines;
  • (\ W: 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.
Copy codeThe Code is as follows:
The "+" metacharacter specifies that the leading character must appear one or more times consecutively.

= For example,/es +/
Match the "tesseessssseast12354haeasashaha" string. First, match the first letter e, and then match s. 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 string "tesseessssseast12354haeasashaha". First, matches the string with the first letter "e", and the second after the string appears zero or consecutively. Check the instance.

"?" Metacharacter specifies that the leading object must appear zero or once consecutively

= For example,/es? /
Matches the string "tesseessssseast12354haeasashaha". First, matches the string with the first letter "e". The second after the string appears zero or at most once (that is, the last letter "s" does not appear repeatedly ).

Sample Code:
Copy codeThe Code is 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 )."
";
?>

Execution result:

In addition, if you think the metacharacter "+ *?" This is hard to understand.{}Method substitution:

For example, es * can be written as es {0,}, while es + can be written as es {1,}, es? Elasticsearch can be written as es {0, 1}. Note: you do not need to write (or use spaces) when you are not sure about the number of times ).

Of course, you must specify the number of occurrences as follows:Es {3}Indicates that s appears three times

Trigger:

For exampleReplace multiple consecutive spaces with one space.In this way:Preg_replace ("/\ s +/", "", $ str );
For exampleReturns the number (integer) in the string):Preg_replace ("/\ d + /","(\ 0) ", $ str);\ 0Yes indicates a string value that complies with the rule.

For exampleReturns the number with the decimal point in the string.:Preg_replace ("/\ d + \. \ d + /","(\ 0) ", $ str); // "\." Indicates the output decimal point

Find a string consisting of letters:Preg_replace ("/[a-zA-Z] + /","(\ 0) ", $ str)

Find a string consisting of letters and digits:Preg_replace ("/([a-zA-Z] | \ d) + /","(\ 0) ", $ str)

 

In the regular expression, the "or" Operation uses "|"

For example, the preceding example is to find a string consisting of letters or numbers.([A-zA-Z] | \ d)You can also write it like this.([A-zA-Z] | [0-9])

"^"Is regarded as a negative operator when it appears in;[^ 0-9]Represents any character except a number.

When "^" is outside "[]" or "[]" is absent, it should be regarded as a positioning character.

The positioning Operator means:"^": Must start with a" The "string; similar to:"En $": $ Indicates that it must end with en.

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.

Related Article

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.