PHP Regular Expressions

Source: Internet
Author: User
Tags php regular expression
What do you mean regular expressions

Regular expressions are a logical formula for manipulating strings by combining certain characters into a regular string called a regular matching pattern.

$p = '/apple/'; $str = "Apple Banna"; if (Preg_match ($p, $str)) {echo ' matched ';}

Where the string '/apple/' is a regular expression that matches the existence of an apple string in the source string.

PHP uses the Pcre library function for regular matching, such as the preg_match used in the previous example to perform a regular match, commonly used to determine whether a class of character patterns exist.

Basic syntax for regular expressions

In the Pcre library function, the regular match pattern consists of delimiters and metacharacters, which can be any character that is non-numeric, non-backslash, and not a space. Frequently used delimiters are forward slashes (/), hash symbols (#), and inverse symbols (~), for example:

/foo bar/#^[^0-9]$#~php~

If the pattern contains delimiters, the delimiter needs to be escaped with a backslash (\).

/http:\/\//

If the pattern contains more split characters, it is recommended that you replace other characters as separators, or you can escape them with preg_quote.

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

The pattern modifier can be used after the delimiter, including: I, M, S, X, and so on, such as using the I modifier to ignore case matching:

$str = "Http://www.imooc.com/", if (Preg_match ('/http/i ', $str)) {echo ' matches successfully ';}

Metacharacters and escape

Characters with special meanings in regular expressions are called metacharacters, and commonly used meta-characters are:

\ generally used to escape characters
^ Assert the starting position of the target (or the beginning of the line in multiline mode)
$ assert the end position of the target (or line ending in multiline mode)
. Match any character except line break (default)
[Start character class definition
] End character class definition
| Start an optional branch
(The start tag of a child group
) The end tag of the child group
? As a quantifier, represents 0 or 1 matches. The greedy character that is used to change quantifiers after quantifiers. (check quantifier)
* quantifier, 0 or more matches
+ quantifier, 1 or more matches
{Custom quantifier start tag
} Custom quantifier end tag

//The \s below matches any whitespace character, including spaces, tabs, newline characters. [^\s] represents a non-whitespace character. [^\s]+ indicates that a non-whitespace character is matched one or more times. $p = '/^ me [^\s]+ (Apple | banana) $/'; $str = "I like to eat apples"; if (Preg_match ($p, $str)) {echo ' matches successfully ';}

The metacharacters has two usage scenarios, one that can be used anywhere and one that can only be used within square brackets, with the following:

\ escape Character
^ indicates that the character class is reversed only when it is the first character (in square brackets)
-Marker Character Range

Where ^ is outside the parentheses, indicating the starting position of the assertion target, but inside the square brackets represents the character class reversed, the minus sign in square brackets-can mark the range of characters, for example, 0-9 represents all numbers between 0 and 9.

//The \w below matches letters or numbers or underscores. $p = '/[\w\.\-]+@[a-z0-9\-]+\. (COM|CN)/'; $str = "My mailbox is Spark.eric@imooc.com";p reg_match ($p, $str, $match); Echo $match [0];

Greedy mode and lazy mode

Each meta-character in a regular expression matches one character, and after using + it will become greedy, it will match as many characters as possible, but with a question mark? character, it will match as few characters as possible, both lazy mode.

Greedy mode: When the match and can not match, the first match

//The \d below indicates a matching number $p = '/\d+\-\d+/'; $str = "My phone is 010-12345678"; Preg_match ($p, $str, $match); echo $match [0];//Result: 010-12345678

Lazy mode: Priority mismatch when matching and mismatches are not matched

$p = '/\d?\-\d?/'; $str = "My phone is 010-12345678";p reg_match ($p, $str, $match); Echo $match [0];//Result: 0-1

You can use {} to specify the number of characters to match when we know exactly the length of the match

$p = '/\d{3}\-\d{8}/'; $str = "My phone is 010-12345678";p reg_match ($p, $str, $match); Echo $match [0];///Result: 010-12345678

Using regular expressions to match

The purpose of using regular expressions is to implement a more flexible approach than string handlers, so it is primarily used to determine whether a substring exists, string substitution, split string, get pattern substring, and so on, just like a string handler function.

PHP uses the Pcre library function for regular processing, by setting the pattern, and then invoking the relevant handler function to get the matching result.

The Preg_match is used to perform a match, which can be simply used to determine whether the pattern matches successfully, or to get a match, and his return value is 0 or 1 of the successful match, which stops the search after 1 times.

$subject = "abcdef"; $pattern = '/def/';p reg_match ($pattern, $subject, $matches);p Rint_r ($matches);//Result: Array ([0 ] = def)

The above code simply performs a match and simply determines whether the DEF matches the success, but the powerful place of the regular expression is the pattern match, so more often, the pattern is used:

$subject = "abcdef"; $pattern = '/A (. *?) D/';p reg_match ($pattern, $subject, $matches);p Rint_r ($matches); The result is: Array ([0] = ABCD [1] = BC)

Regular expressions can be used to match a pattern to get more useful data.

Find all matching results

Preg_match can only match one result at a time, but many times we need to match all the results, Preg_match_all can iterate over the array of matching results for a list.

$p = "|<[^>]+> (. *?) ] +>|i "; $str =" Example: This is a test ";p reg_match_all ($p, $str, $matches);p Rint_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
";p Reg_match_all ($p, $str, $matches);p Rint_r ($matches);

$matches results sorted to $matches[0] saves all matches for the full pattern, $matches [1] saves all matches for the first subgroup, and so on.

Search and replace of regular expressions

The search and substitution of regular expressions have important uses in some aspects, such as adjusting the format of the target string, changing the order of matching strings in the target string, and so on.

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

$string = ' April ', $pattern = '/(\w+) (\d+), (\d+)/I '; $replacement = ' $ $, ${1} "; Echo Preg_replace ($pattern, $replacement, $string); The result is: April

Where ${1} is equivalent to the $ $ notation, which represents the first matched string, which is the second matching one.

With complex patterns, we can replace the contents of the target string more precisely.

$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 equivalent to $4, and so on Echo preg_replace ($patterns, $replace, ' {startdate} = 1999-5-27 '); The result is: $startDate = 5/27/1999//detailed explanation of the result: (19|20) means to take 19 or 20 of any number, (\d{2}) represents two numbers, (\d{1,2}) represents 1 or 2 digits (\d{1,2}) Represents a 1 or 2 number. ^\s*{(\w+) \s*=} represents a character that begins with any space and contains the characters in {} and ends with any space, with the last = sign.

Use regular substitution to remove extra spaces and characters:

$ str = ' one '; $str = preg_replace ('/\s+/', ' ', $str); Echo $str; Result changed to ' one ', '

regular match common case

regular match often on form validation, some fields have some formatting requirements, such as User names are generally required to be letters, numbers or underscores, mailboxes, telephones and so on have their own rules, so use regular expressions can be very good validation of these fields.

Copyright notice: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the PHP regular expression, including the content, I hope the PHP tutorial interested in a friend to help.

  • 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.