Php regular expression entry (1/3)

Source: Internet
Author: User
Tags character set ereg lowercase php regular expression php tutorial posix regular expression

1. Basic knowledge of regular expressions
Meaning: string mode consisting of common characters (a-z) and special characters
Function: validity verification.
Replace text.
Extract a substring from a string.
Classification: POSIX and Perl
POSIX is easier to master, but cannot be used in binary mode. perl is relatively complex.
2. POSIX regular expression
1. Write regular expressions
Table 4.3 POSIX regular expression syntax format list
Character
Description
 

Escape character, used to escape special characters. For example, '.' matches a single character, and '.' matches a dot. '-' Match the hyphen '-', ''match the symbol''
 
^
Matches the start position of the input string. For example, '^ hes' indicates a string starting with' he '.
 
$
Matches the end position of the input string. For example, 'OK $' indicates the string ending with 'OK'.
 
*
Matches the previous subexpression zero or multiple times. For example, 'Zo * 'can match "z" and "zoo ". * Equivalent to {0 ,}
 
+
Match the previous subexpression once or multiple times. For example, 'Zo + 'can match "zo" and "zoo", but cannot match "z ". + Equivalent to {1 ,}
 
?
Match the previous subexpression zero or once. For example, 'Do (es )? 'Can match "do" in "do" or "does ". '? 'Is equivalent to {0, 1}
 
{N}
N is a non-negative integer. Match n times. For example, 'O {2} 'cannot match 'O' in "Bob", but can match two 'O' in "food'
 
{N ,}
N is a non-negative integer. Match at least n times. For example, 'O {2,} 'cannot match 'O' in "Bob", but can match all 'o' in "foooood '. 'O {1,} 'is equivalent to 'O + '. 'O {0,} 'is equivalent to 'O *'
 
{N, m}
Both m and n are non-negative integers, where n ≤ m. Match at least n times and at most m times. For example, "o {1, 3}" matches the first three 'O' in "fooooood '. 'O {0, 1} 'is equivalent to 'O? '. Note that there must be no space between a comma and two numbers.
 
?
When this character is followed by any other delimiter (*, + ,?, The matching mode after {n}, {n ,}, {n, m}) is not greedy. The non-greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible. For example, for strings "oooo", 'O ++? 'Will match a single "o", and 'O +' will match all 'O'
 
.
Match any single character except "n". To match any character including 'n', use the '[. n]' mode.
 
(Pattern)
Match pattern and obtain this match. Save the obtained match to the corresponding array. To match parentheses, use '(' or ')'
 
(? : Pattern)
Matches pattern but does not get the matching result. That is to say, this is a non-get match and is not stored. This is useful when "or" | "is used to combine various parts of a mode. For example, 'industr (? : Y | ies). It is a simpler expression than 'industry | industries '.
 
(? = Pattern)
Forward pre-query: matches the search string at the beginning of any string that matches the pattern. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows (? = 95 | 98 | NT | 2000) 'can match "Windows" in "Windows 2000", but cannot match "Windows" in "Windows 3.1 ". Pre-query does not consume characters. That is to say, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.
 
(?! Pattern)
Negative pre-query: matches the search string at the beginning of any string that does not match pattern. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows (?! 95 | 98 | NT | 2000) 'can match "Windows" in "'Windows 3.1", but cannot match "Windows" in "Windows 2000 ". Pre-query does not consume characters. That is to say, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.
 
X | y
Match x or y. For example, if 'Z | food' matches "z" or "food", '(z | f) ood' Matches "zood" or "food"
 
[Xyz]
Character Set combination. Match any character in it. For example, '[abc]' can match 'A' in "plain'
 
[^ Xyz]
Negative value character set combination. Match any character not included. For example, '[^ abc]' can match 'P' in "plain'
 
[A-z]
Character range. Matches any character in the specified range. For example, '[a-z]' can match any lowercase letter in the range of 'A' to 'Z '.
 
[^ A-z]
Negative character range. Match any character that is not within the specified range. For example, '[^ a-z]' can match any character that is not in the range of 'A' to 'Z '.
 
Here are some examples of simple regular expressions:
● '[A-Za-z0-9]': indicates all uppercase letters, lowercase letters, and numbers ranging from 0 to 9.
● '^ Hello': a string starting with "hello.
● 'World $ ': a string ending with 'world.
● '. At': a string that starts with any single character other than "n" and ends with "at", such as "cat" and "nat.
● '^ [A-zA-Z]': a string starting with a letter.
● 'Hi {2} ': indicates the letter h followed by two I, namely, hii.
● '(Go) +': indicates a string containing at least one 'go' string, such as 'gogogo'
The ID card number is generally composed of 18 or 17 digits followed by an X or Y letter. To match the ID card number, you can write:
^ [0-9] {17} ([0-9] | X | Y) $
The regular expression of the Email address can be written as follows:
^ [A-zA-Z0-9-] + @ [a-zA-Z0-9-] +. [a-zA-Z0-9-.] + $
2. String matching
Ereg () and eregi () functions
You can use the ereg () function to find matching conditions between a string and a sub-string, return the length of the matching string, and return an array of matching characters with parameters. The syntax format is as follows:
Int ereg (string ($ pattern), string $ string [, array $ regs])
Copy the code as follows:

<? Php Tutorial
/* This example checks whether the string is a date in ISO format (YYYY-MM-DD )*/
$ Date = "1988-08-09 ";
$ Len = ereg ('([0-9] {4})-([0-9] {1, 2})-([0-9] {1, 2 })', $ date, $ regs); // The date format is YYYY-MM-DD
If ($ len)
{
Echo "$ regs [3]. $ regs [2]. $ regs [1]". "<br>"; // output "09.08.1988"
Echo $ regs [0]. "<br>"; // output "1988-08-09"
Echo $ len; // output 10
}
Else
{
Echo "incorrect date format: $ date ";
}
?>

Homepage 1 2 3 Last page

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.