Home  >  Article  >  Backend Development  >  Introduction to regular expression functions in PHP_PHP tutorial

Introduction to regular expression functions in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:20:20976browse

Regular Expression

Regular expression system:
1. POSIX
2. Perl

The regex used in PHP is PCRE:
NOTE: PCRE (Perl Compatible Regular Expressions)

PCRE syntax:
1. Delimiter
Must appear in pairs, you can use anything except 0-9a-zA-Z Character
2. Atom
1. The visible and invisible characters that the regular expression needs to match are all atoms
2. A regular expression contains at least one atom
3. When it needs to match such as "(", "[", "^" and other semantic symbols need to be escaped with "" backslash

  Atomic characters:
  f matches the form feed character
n matches the newline character
r matches the carriage return character
t matches the tab character
v matches the vertical tab character

3. Metacharacters
Escape character
^ matches the beginning of the string
$ Matches the end of the string
Matches any single character except "n"

* Matches the previous subexpression 0 or more times
+ Matches the previous subexpression 1 time or multiple times
 ? Match the previous subexpression 0 or 1 times

 {n} Match n times
  {n,} Match n times or more than n times
  {n, m} matches at least n times and at most m times, (n<=m)

 [] The square brackets represent the atom table, and the atom status in the middle is equal when matching. Characters
 [^] circumflex character, excludes characters contained in the following atom table.

 (pattern) Matches pattern and obtains this match.
 num refers to the obtained numth match. .


 (?:pattern) Match pattern but do not get this match

 (?=pattern) Positive pre-check, non-get matching, for example: windows(?= XP|7) can match windows in windows windows, cannot match windows in windows98
 (?<=pattern) Reverse positive pre-check, non-obtaining matching. For example: (?<=My|Postgre)SQL can match the SQL in MySQL, but cannot match the SQL in MSSQL
  (?
 b matches word boundaries
 B matches characters other than word boundaries
  🎜> d matches any number. Equivalent to [0-9]
D matches any non-digit character. Equivalent to [^0-9]

 s matches any whitespace character (including space, tab, form feed, etc.). Equivalent to [fnrtv]
S matches any non-whitespace character. Equivalent to [^fnrtv]
 
 w matches any number, letter, or underscore. Equivalent to [0-9a-zA-Z]
 W matches any character that is not a number, letter, or underscore. Equivalent to [^0-9a-zA-Z]

4. Mode modifier
i is not case sensitive
m If there is a carriage return or line feed in this mode, ^ and $ will Match the beginning and end of each line
s Let . match n
The carriage return is not ignored at the end. When there is a $ symbol at the end, if you add a carriage return after the matched string, $ can still match it successfully. But after adding D, the carriage return at the end no longer matches

NOTE: Regular expressions match from left to right


Related functions:
preg_filter — Perform a regular expression search and replace
preg_grep — Returns the array entries matching the pattern
preg_last_error — Returns the error code produced by the last PCRE regular expression
preg_match_all — Performs a global regular expression match
preg_match — Perform a regular expression match
Preg_quote — Escape regular expression characters
Preg_replace_callback — Perform a regular expression search and replace using a callback
Preg_replace — Perform a regular expression search and replace
preg_split — Split a string by a regular expression



http://www.bkjia.com/PHPjc/325110.html

www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/325110.htmlTechArticleRegular Expression (Regular Expression) Regular expression system: 1.POSIX 2.Perl The regex used in PHP is PCRE: NOTE:PCRE(Perl Compatible Regular Expressions) PCR...
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