Php regular expression usage

Source: Internet
Author: User
Php regular expression usage
How to use regular expressions in php programming? Php regular expression usage. For more information, see.

Content of this section:Php regular expression

Regular expressions are powerful tools for pattern matching and replacement.

You can find ta in UNIX-based tools. In addition, client scripting languages such as javascript are also supported.

Regular expressions have exceeded the limits of a language or system and become a widely accepted concept and function. in many php tutorials, this must be introduced.

The following are some common parameters and formats of regular expressions in php.

Syntax format: Between the delimiters. Frequently used metacharacters include "+", "*", and "?". Where,

The "+" metacharacter specifies that its leading character must appear one or more times consecutively in the target object, the "*" metacharacter specifies that the leading character must appear zero or multiple times in the target object, and "?" Metacharacter specifies that the leading object must appear zero or once consecutively in the target object.

/Jim {2, 6 }/
The above regular expression specifies that the character m can appear 2-6 times consecutively in the matching object. Therefore, the above regular expression can match strings such as jimmy or jimmm.pdf.
After a preliminary understanding of how to use regular expressions, let's take a look at the usage of several other important metacharacters.

\ S: used to match a single space character, including the tab key and line break;
\ S: used to match all characters except a single space character;
\ D: used to match numbers from 0 to 9;
\ W: used to match letters, numbers, or underscores;
\ W: used to match all characters that do not match \ w;
.: Used to match all characters except line breaks.

In addition:

Commonly used positioning characters include "^", "$", "\ B", and "\ B ". The "^" operator specifies that the matching mode must start with the target string, and the "$" operator specifies that the matching mode must end with the target object, the \ B locator specifies that the matching mode must appear at either the beginning or end of the target string, the "\ B" locator specifies that the matched object must be within the boundary of the start and end of the target string. that is, the matched object cannot start with the target string, it cannot end with the target string. Matches a specified range, not limited to specific characters. For example:/[A-Z]/The above regular expression will match any uppercase letter from A to Z. /[A-z]/The above regular expression will match any lowercase letter in the range from a to z. /[0-9]/The above regular expression will match any number from 0 to 9. /([A-z] [A-Z] [0-9]) +/

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.