Regular Expressions in PHP

Source: Internet
Author: User
Tags alphanumeric characters
This article mainly introduces the regular expression in PHP, has a certain reference value, now share to everyone, the need for friends can refer to

Simple introduction to regular expressions and their functions

Ø understanding of regular expressions

n Regular expressions are a custom language rule that describes the pattern of string permutations

n do not use regular expressions if you can use a task that is done with a string-handling function

n There are some complex operations that can only be used with regular expressions

n Regular Expressions are also known as a pattern expression

The n regular expression is to compare, divide, find, replace, and so on with the input string information through a pattern with a specific rule .

Ø characteristics of regular Expressions

n The regular expression is also a string

n a string consisting of a string of special meaning

N has a certain writing rule and is also a pattern

n is considered to be a programming language: A string that is written according to a rule in a particular character, forming a pattern

Note: If a regular expression is not used with a function, it is a string, and if the regular expression is used in a function, the true function of the regular expression can be played .

two sets of regular expression function libraries are available in the øphp

Posix extended Regular expression ereg_ functions beginning with

perl - compatible regular expression preg_ functions that begin with

Recommended Use Prel compatible Regular expression function libraries

Syntax for regular expressions

Ø Regular delimiter

o atoms in regular expressions

Ø metacharacters in regular expressions

Ø pattern modifier for regular Expressions

Regular delimiter

The regular delimiter is the symbol used to declare the bounds of the regular expression.

Regular expressions are commonly used as a boundary character/, and any character other than anon-numeric letter and backslash \ can be a bounding character of a regular expression.

Note: A complete regular expression has two bounding characters

N one is a string-type quotation mark boundary character '

N one is the bounding character of the regular expression //( General /)

n the whole is '//'

The atom of the regular expression

the smallest unit that makes up a regular expression is the atom

Regular numbers of letters are atoms, and most of the invisible characters are atoms.

For example: \n\r\t ... These translatable characters are all elements

Ø the atoms in the regular

\d represents a number between 0-9

\d represents any character except the 0-9

\s that matches any one of the white-space characters including \n\r\t.

\s represents any character except whitespace or translatable characters

\w represents the number of uppercase and lowercase characters and any character in an underscore

\w means any character except uppercase and lowercase alphanumeric characters and underscores

Metacharacters (atomic modifier) in a regular expression

Ø [] Atomic list

indicates that an atom is selected in the specified character

For example :[5-9] means 5 6 7 8 9 consecutive characters allowed to be abbreviated as : [ start character - end character ]

Ø [^] Exclude List

For example: [^4] indicates that in addition 4 any one of the characters can be

Ø using meta-characters (atomic modifiers) to simulate atoms

\d [0-9] \d [^0-9]

\s [\n\r\t] \s[^\n\r\t]

\w[a-za-z0-9] \w[^a-za-z0-9]

Ø atomic number modifier

? indicates that the previous atom appears 0 or 1 times .

+ indicates that the preceding atom appears 1 or more times

* indicates that the previous atom appears 0 or more times

Ø number of atoms modified list

n {m,n} indicates that the preceding atom has a minimum of m times, with a maximum of n occurrences

n {m,} indicates that the preceding Atom appears at least m times

n {m} indicates that the preceding Atom can only appear m Times

Ø number of atoms modifier list analog quantity modifier

? {0,1 }

+ {1,}

* {0,}

Ø character string boundary modifier

^ denotes content that begins with a specified character

$ denotes content that ends with a specified character

Attention:

^ $ one after the previous to indicate whether the content is consistent between the two

If you want to use ^ and $ to match the start and end parts of each line, you need to use the analog modifier m

Ø | selection Modifier

n or similar

Ø () mode unit

n Change the priority level

N Treats a multi-atom as an atom, you can use the atomic number modifier

N temporarily save the contents of the matching parentheses as in-memory

N if you are using () just to simulate an atom using an atomic number modifier, you can use it at the beginning of the parenthesis .: to touch the content of the role, improve efficiency

n You can reverse-reference the contents of the parentheses with the help of the function

Ø Meta-character

. (dot) match except \ n any one character outside of the

Regular expression Simulation modifier

Ø i ignore case

Ø x ignores whitespace in regular expressions

Ø s used . ( meta-character ) can match to \ n

Ø S acceleration Match

Ø U mode change greedy mode

change greedy mode, the program defaults to greedy mode, match the furthest end, we can use in the regular *? use non-greedy mode while matching any character

Note: If the program is greedy mode at this time, use the U , the program becomes non-greedy mode. If the program is not greedy at this point , the program becomes greedy after you use U

Functions of regular expressions

Ø Preg_grep () returns the array cell that matches the pattern

@param1 : Regular Expressions

@param2 : The array to match

@param3 : Optional Parameters the default is false. Set to true to return an array cell that is reversed from pattern matching

Return value: Returns the array cell that matches the pattern

Ø Preg_match () make a regular match suitable for making inquiries there is no

@param1 : Regular Expressions

@param2 : the string to match

@param3 : Optional parameters that return content that matches the regular expression

return value: Successfully returned 1, failure returned 0 1 indicates number of matches

Ø Preg_match_all () make a regular match how many are suitable for querying

@param1 : Regular Expressions

@param2 : the string to match

@param3 : Optional parameters that return content that matches the regular expression

Return value: Returns the number of matches, or 0 if no match is taken

Ø Preg_split () separating strings with regular expressions

@param1 : Regular Expressions

@param2 : the string to separate

@param3 : returns the delimited array

Ø preg_replace () perform the substitution of regular expressions

@param1 : To find the regular expression

@param2 : What to replace

@param3 : Where to perform the replace operation (array or string)

Return value: Returns the replaced array or string

Related Article

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.