Php regular expression

Source: Internet
Author: User
Tags php regular expression
: This article mainly introduces php regular expressions. if you are interested in PHP tutorials, you can refer to them. What is a regular expression?

A regular expression is a logical formula used to operate strings. it combines certain characters into a regular string and is called the regular expression matching mode.

$ P = '/apple/'; $ str = "apple banna"; if (preg_match ($ p, $ str) {echo 'matched ';}

The string '/apple/' is a regular expression used to match whether an apple string exists in the source string.

In PHP, the PCRE library function is used for regular expression matching. for example, preg_match in the above example is used to execute a regular expression matching. it is often used to determine whether a type of character pattern exists.

Basic regular expression syntax

In the PCRE library function, regular expression matching uses separators and metacharacters. separators can be non-numeric, non-diagonal lines, and non-space characters. Frequently used delimiters are forward slashes (/), hash symbols (#), and reverse symbols (~), For example:

/Foo bar/# ^ [^ 0-9] $ #~ Php ~

If the mode contains delimiters, the delimiters must be escaped using a backslash.

/Http :\/\//

If the mode contains many delimiter characters, we recommend that you replace other characters as separators, or use preg_quote for escape.

$ P = 'http: // '; $ p ='/'. preg_quote ($ p,'/').'/'; echo $ p;

You can use a pattern modifier after the separator. the pattern modifier includes: I, m, s, and x. for example, you can use an I modifier to ignore case-insensitive matching:

$ Str = "Http: // www.imooc.com/"; if (preg_match ('/http/I', $ str) {echo 'matched successfully ';}

Metacharacter and escape

The characters with special meanings in regular expressions are called metacharacters. common metacharacters include:

\ Is generally used for escape characters
^ Start position of the assertion target (or the first row in multi-row mode)
$ End position of the assertion target (or end of a row in multi-row mode)
. Match any character except the line break (default)
[Start character class definition
] End character class definition
| Start an optional branch
(Start mark of the sub-group
) End tag of the sub-group
? As a quantizer, it indicates 0 or 1 match. It is behind the quantifiers to change the greedy nature of quantifiers. (Query quantifiers)
* Quantifiers, matching 0 or multiple times
+ Quantifiers, matching once or multiple times
{Start tag of custom quantifiers
} Custom quantifiers end mark

// The following \ s matches any blank characters, including spaces, tabs, and line breaks. [^ \ S] indicates a non-blank character. [^ \ S] + indicates that a non-blank character is matched once or multiple times. $ P = '/^ I [^ \ s] + (Apple | banana) $/'; $ str = "I like Apple"; if (preg_match ($ p, $ str) {echo 'matched successfully ';}

Metacharacters can be used in either of the following scenarios:

\ Escape characters
^ If it is used only as the first character (inside square brackets), it indicates that the character class is reversed.
-Mark character range

^ Indicates the start position of the assertion target, but inside the square brackets, it indicates the inverse of the character class. the minus sign in the square brackets can mark the character range, for example, 0-9 indicates all numbers between 0 and 9.

// The following \ w matches letters, numbers, or underscores. $ P = '/[\ w \. \-] + @ [a-z0-9 \-] + \. (com | cn)/'; $ str = "my mailbox is a Spark.eric@imooc.com"; preg_match ($ p, $ str, $ match); echo $ match [0];

Greedy mode and lazy mode

In a regular expression, each metacharacter matches one character. When + is used, it will become greedy. it will match as many characters as possible, but will use question marks? It matches as few characters as possible, which is both a lazy mode.

Greedy mode: Priority is given to matching and mismatch.

// The following \ d indicates the matching number $ p = '/\ d + \-\ d +/'; $ str = "my phone number is 010-12345678 "; preg_match ($ p, $ str, $ match); echo $ match [0]; // The result is 010-12345678.

Lazy mode: when matching or not matching, the priority does not match

$ P = '/\ d? \-\ D? /'; $ Str = "my phone number is 010-12345678"; preg_match ($ p, $ str, $ match); echo $ match [0]; // The result is: 0-1

When we know exactly the length of the matched characters, we can use {} to specify the number of matching characters.

$ P = '/\ d {3} \-\ d {8}/'; $ str = "my phone number is 010-12345678"; preg_match ($ p, $ str, $ match); echo $ match [0]; // The result is: 010-12345678.

Use regular expressions for matching

Regular expressions are used to implement more flexible processing methods than string processing functions. Therefore, they are the same as string processing functions, it is mainly used to determine whether a sub-string exists, replace the string, split the string, and obtain the mode sub-string.

PHP uses the PCRE library function for regular expression processing. by setting the mode, it calls the relevant processing function to obtain matching results.

Preg_match is used to execute a match. it can be used to determine whether the pattern matches successfully or obtain a matching result. the returned value is the number of successful matches (0 or 1, the search will be stopped once.

$ Subject = "abcdef"; $ pattern = '/def/'; preg_match ($ pattern, $ subject, $ matches); print_r ($ matches); // The result is: array ([0] => def)

The above code simply executes a match, and simply determines whether def can be matched successfully. However, the strong aspect of the regular expression is pattern matching. Therefore, when there are more, usage mode:

$ Subject = "abcdef"; $ pattern = '/(.*?) D/'; preg_match ($ pattern, $ subject, $ matches); print_r ($ matches); // The result is: array ([0] => abcd [1] => bc)

A regular expression can match a pattern to obtain more useful data.

Search all matching results

Preg_match can only match the results once, but many times we need to match all the results. preg_match_all can obtain a list of matching result arrays cyclically.

$ P = "| <[^>] +> (.*?) ] +> | I "; $ str =" Example:

This is a test

"; Preg_match_all ($ p, $ str, $ matches); print_r ($ matches );

You can use preg_match_all to match the data in a table:

$ P = "/(.*?) <\/Td> \ s *(.*?) <\/Td> \ s * <\/tr>/I "; $ str ="
Eric 25
John 26
"; Preg_match_all ($ p, $ str, $ matches); print_r ($ matches );

$ Matches results are sorted as $ matches [0] to save all the matches in the full mode. $ matches [1] stores all the matches in the first subgroup, and so on.

Search and replace regular expressions

Regular expression search and replacement are important in some aspects, such as adjusting the format of the target string and changing the order of matching strings in the target string.

For example, we can simply adjust the date format of the string:

$ String = '0000l 15,201 4'; $ pattern = '/(\ w +) (\ d +), (\ d +)/I '; $ replacement = '$3, $ {1} $ 2'; echo preg_replace ($ pattern, $ replacement, $ string); // The result is 2014, limit L 15.

$ {1} is equivalent to $1, which indicates the first matching string and $2 indicates the second matching string.

Through the complex mode, we can replace the content of the target string more accurately.

$ Patterns = array ('/(19 | 20) (\ d {2})-(\ d {1, 2})-(\ d {1, 2 })/', '/^ \ s * {(\ w +)} \ s * =/'); $ replace = array ('\ 3/\ 4/\ 1 \ 2 ', '$ \ 1 ='); // \ 3 is equivalent to $3, \ 4 is equivalent to $4, and echo preg_replace ($ patterns, $ replace, '{startDate} ='); // The result is: $ startDate = 5/27/1999 // The result is as follows: (19 | 20) represents any number in 19 or 20, (\ d {2}) represents two numbers, (\ d {1, 2}) represents one or two numbers, (\ d {1, 2}) indicates one or two numbers. ^ \ S * {(\ w +) \ s * =} indicates characters starting with any space and included in {}, ending with any space, finally, there is a =.

Use regular expression replacement to remove extra spaces and characters:

$ Str = 'one two'; $ str = preg_replace ('/\ s +/', '', $ str); echo $ str; // The result is changed to one two'

Common cases of regular expression matching

Regular Expression Matching is often used in form verification. some fields have certain format requirements. for example, a user name must be composed of letters, numbers, or underscores, email and phone numbers all have their own rules. Therefore, regular expressions can be used to verify these fields.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the php regular expression, including the content, and hope to be helpful to friends who are interested in the PHP Tutorial.

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.