Learning Regular Expressions in php

  • 2021-07-13 04:40:36
  • OfStack

Syntax format: between the delimiter "/".

Commonly used metacharacters include: "+", "*", and "?" .

Among them,

The "+" metacharacter specifies that its leading character must appear one or more times in a row in the target object,

The "*" metacharacter specifies that its leading character must appear zero or several times in a row in the target object,

And "?" Metacharacter specifies that its leading object must appear in the target object zero or once in a row.

/jim{2,6}/ < br/ >
The regular expression specifies that the character m can appear 2-6 times in succession in the matching object, so the regular expression can be matched with a string such as jimmy or jimmmmmy. < br/ >
After getting a glimpse of how to use regular expressions, let's look at the use of several other important metacharacters. < br/ >
\ s: Used to match a single space character, including the tab key and the newline character; < br/ >
\ S: Used to match all characters except a single space character; < br/ >
\ d: Used to match numbers from 0 to 9; < br/ >
\ w: Used to match letters, numbers, or underscore characters; < br/ >
\ W: Used to match all characters that do not match\ w; < br/ >
.: Used to match all characters except newline characters. < br/ >

In addition:

Common locators include: "^", "$", "\ b", and "\ B".

The "^" locator specifies that the match 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 match pattern must appear at one of the two bounds at the beginning or end of the target string,

The "\ B" locator specifies that the matching object must be within the bounds of the beginning and end of the target string, that is, the matching object cannot be the beginning or end of the target string.

Matching specifies a range of 1 and is not limited to specific characters. For example:

/[A-Z]/
The above regular expression will match any 1 capital letter in the range from A to Z.
/[a-z]/
The above regular expression will match any 1 lowercase letter from a to z.
/[0-9]/
The above regular expression will match any 1 number in the range from 0 to 9.
/([a-z][A-Z][0-9])+/


Related articles: