Home >Backend Development >PHP Tutorial >Introduction to php regular expression usage

Introduction to php regular expression usage

WBOY
WBOYOriginal
2016-07-25 08:52:56866browse
How to use regular expressions in PHP programming? PHP regular expression usage, please refer to it if you need it.

Contents of this section: php regular expression

Regular expressions are a powerful tool for pattern matching and replacement.

He can be found in tools based on UNIX systems. In addition, client-side scripting languages ​​such as JavaScript also provide support.

Regular expressions have gone beyond the limitations of a certain language or a certain system and have become a widely accepted concept and function. In many PHP tutorials, this is what must be introduced.

The following are some common parameters and formats of regular expressions in PHP for your reference.

Syntax format: between the delimiters "/". The more commonly used metacharacters include: "+", "*", and "?". in,

The "+" metacharacter stipulates that its leading character must appear one or more times continuously in the target object. The "*" metacharacter stipulates that its leading character must appear zero times or multiple times in a row in the target object. The "?" metacharacter stipulates that its leading object must appear zero or once in the target object.

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

s: used to match a single space character, including tab key and newline character;
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 underscore characters;
W: used to match all characters that do not match w;
. : Used to match all characters except newline characters.

Also:

The more commonly used locators include: "^", "$", "b" and "B". The "^" locator specifies that the matching pattern must appear at the beginning of the target string, The "$" locator specifies that the matching pattern must appear at the end of the target object. The b locator specifies that the matching pattern must appear at one of the two boundaries at the beginning or end of the target string, The "B" locator stipulates that the matching object must be located within the two boundaries of the beginning and end of the target string, that is, the matching object cannot be used as the beginning or the end of the target string. Matches a specified range and is 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])+/


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn