Home  >  Article  >  Backend Development  >  java regular expression introduction to regular expression functions in PHP

java regular expression introduction to regular expression functions in PHP

WBOY
WBOYOriginal
2016-07-29 08:48:10969browse

Regular Expression (Regular Expression)
Regular expression system:
 1.POSIX
 2.Perl
The regex used in PHP is PCRE:
 NOTE: PCRE (Perl Compatible Regular Expressions, Perl Compatible Regular Expressions)
PCRE syntax:
 1. Delimiters
  Must appear in pairs, you can use any character except 0-9a-zA-Z
 2. Atom
 1. The visible and invisible characters that the regular expression needs to match are all atoms
 2. A regular expression The expression contains at least one atom
  3. When you need to match semantic symbols such as "(", "[", "^" and other semantic symbols, you need to use "" backslash to escape
   Atomic character:
   f matches the form feed character
  n matches newline character
  r matches carriage return character
  t matches tab character
  v matches vertical tab character
 3. Metacharacter
  Escape character
  ^ matches the beginning of the string
  $ matches the end of the string
 . match Any single character except "n"
   * Matches the previous subexpression 0 or more times
   + Matches the previous subexpression 1 or more times
  ? Matches the previous subexpression 0 or 1 times
   { n} matches n times
  {n,} matches n times or more
  {n,m} matches at least n times and at most m times, (n<=m)
 [] The square brackets represent the atom table, the atoms in the middle The status is equal. When matching, match any character in the table
  [^] and exclude the characters contained in the following atomic table.
  (pattern) matches pattern and obtains this match. The numth matching reference.
  (?:pattern) matches pattern but does not obtain this match
                               ute quo=pattern)                         utebilt (?=pattern)                          lainvicous (?=pattern)  Matches pattern but does not get this match Windows in Windows
  (?<=pattern) Reverse affirmative search, non-obtaining matching. For example: (?<=My|Postgre)SQL can match SQL in MySQL, but cannot match 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 character other than a number. 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. Pattern modifier
  i is not case-sensitive
  m If there is a carriage return or line feed in this pattern, ^ and $ will match the beginning and end of each line.
  s allows . to match n
 If you add a carriage return after the string, $ can still match it successfully. But after adding D, the carriage return at the end no longer matches
NOTE: Regular expressions are matched from left to right
Related functions:
preg_filter — perform a regular expression search and replace
preg_grep — return the matching pattern Array entries
  preg_last_error — Returns the error code generated by the last PCRE regular expression
  preg_match_all — Performs a global regular expression match
Preg_replace — Perform a regular expression search and replace using a callback
Preg_split — Split a string by a regular expression
The above has introduced the regular expression function in java regular expression in PHP, including the content of java regular expression. I hope it will be helpful to friends who are interested in PHP tutorials.


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