1. Introduction and function of regular expressions.
01. What is a regular expression?
Regular Expressions (English: Regular Expression, regex, or RegExp, abbreviated as RE), also translated as regular representations, general representations, in computer science, refers to a single string that describes or matches a sequence of strings that conform to a certain syntactic rule. In many text editors or other tools, regular expressions are often used to retrieve and/or replace text content that conforms to a pattern. Many programming languages support the use of regular expressions for string manipulation.
Rule syntax
02. Main function: Split, match, find, replace.
| An expression |
Match |
|
/^s*$/ |
Matches a blank line. |
|
/d{2}-d{5}/ |
Verify the ID number consisting of two digits, one hyphen, and 5 digit digits. |
|
/<s* (s+) (s[^>]*)? >[ss]*<s*/1s*>/ |
Matches HTML tags. |
two common regular functions in 2.PHP.
The Preg_match regular function, based on the Perl prophecy. (More efficient, you need to customize a start terminator.) )
Ereg regular functions, based on POSIX (Uniox, Script).
3. The elements included in the regular expression.
01. Atomic (Ordinary character: A-Z-A-Z 0-9, Atomic table, escape character).
02. Atomic character (characters with special functions).
03. Pattern modifier (System built-in partial module, similar function).
4. "Atoms" in regular expressions.
01.a-z A-Z _ 0-9//most common characters.
(ABC) (SKD)//cell notation enclosed in parentheses.
03.[ABCS] [^ABD]//greedy match, enclosed in square brackets from the table, the atomic table ^ represents the exclusion or the opposite content.
04. Escape characters (case sensitive)
D contains all numbers = = [0-9].
D does not contain all numbers = = [^0-9].
W contains all english characters = = [A-za-z_0-9].
W does not contain all English characters & numbers to match the special symbol = = [^a-za-z_0-9].
s contains blank areas such as carriage return, line feed, page break = = [FNR].
Metacharacters
* 0 times 1 or more occurrences of the previous content
. Matches 0 times 1 or more times of content, but does not include carriage return line wrapping
+ 1 or more times to match the previous content
? Match 0 or 1 times before the previous content
| Select match similar to PHP | (because this operation conforms to a weak type leading to the most complete match in the front)
^ Match String header content
$ Match String Tail content
b matches the word boundary, the boundary can be a space or a special match
B matches unexpected content except with Word boundary
{m} matches the previous content with a repeat number of M times
{m,} repeat times greater than or m times to match previous content
{M,n} matches the repeat number of the previous content m times to N times
() Merge the overall match, and put in memory, you can use 1 2 ... Sequentially get
Instance:
| The code is as follows |
Copy Code |
|
<?php
$mode = "#test #"; This can be done by using the atomic table above.
$str = "SDFSSTESTDF";
if (Preg_match ($mode, $str, $end)) {//mode regular module, str regular content, end regular result, array output.
echo "Match succeeded". $end [0];
} else {
echo "Match failed";
}
?> |
Common regular
* 1, ^s+[a-z a-z]$ can not be empty can not be a space can only be English letters
* 2, S{6, can not be empty for more than six digits
* 3, ^d+$ can not have a space can not be not a digital
* 4, (. *) (. Jpg|. BMP) $ can only be JPG and BMP format
* 5, ^d{4}-d{1,2}-d{1,2}$ can only be 2004-10-22 format
* 6, ^0$ at least one of the selected
* 7, ^0{2,}$ select at least two
* 8, ^[s| s]{20,}$ cannot be empty for more than 20 words
* 9, ^+? [A-z0-9] (([-+.]| [_]+)? [a-z0-9]+) *@ ([A-z0-9]+ (. | -) +[a-z]{2,6}$ Mail
* 10, w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) * ([,;] s*w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *) * Enter multiple addresses separate messages with commas or spaces
* 11, ^ ([0-9]+)]? [0-9] {7,8}$ Phone number 7-bit or 8-bit or preceded by an area code such as (022) 87341628
* 12, ^[a-z-Z 0-9 _]+@[a-z-z 0-9 _]+ (. [ A-Z 0-9 _]+) + (, [A-Z-0-9 _]+@[a-z-A-Z
0-9 _]+ (. [ A-Z 0-9 _]+) + *$
* Can only be letters, numbers, underscores; must have @ and. Simultaneous format to standardize messages
* ^w+@w+ (. w+) + (, w+@w+ (. w+) +) *$ The above expression can also be written in this way, more concise.
^w+ (-w+) | (. w+)) *@w+ ((. | -) *.w+$ [/size] w+)
Matching regular expressions for Chinese characters: [U4E00-U9FA5]
match a specific number:
^[1-9]d*$//Matching positive integer
^-[1-9]d*$//matching negative integers
^-? [1-9]d*$//matching integer
^[1-9]d* |0$//matching nonnegative integer (positive integer + 0)
^-[1-9]d* |0$//matching non positive integer (negative integer + 0)
^[1-9]d*.d* |0.d*[1-9]d*$//matching positive floating-point numbers
^-([1-9]d*.d* |0.d*[1-9]d*) $//matching negative floating-point number
^-? ([1-9]d*.d* |0.d*[1-9]d* |? 0+ |) $//matching floating-point number
^[1-9]d*.d* |0.d*[1-9]d* |? 0+ |0$//matching nonnegative floating-point number (positive floating-point number + 0)
^ (-([1-9]d*.d* |0.d*[1-9]d*)) | 0+ |0$//matching non-positive floating-point numbers (negative floating-point number + 0)
Consists of a letter a~z (case-insensitive), a numeric 0~9, a minus sign, or an underscore
You can only start and end a number or letter with a 4~18 character length
| The code is as follows |
Copy Code |
|
^[a-za-z0-9]{1}[a-za-z0-9|-|_]{2-16}[a-za-z0-9]{1}$ |
The user name is uppercase or lowercase letters or underscores and begins with a letter with a length of 6-20
| The code is as follows |
Copy Code |
|
^[A-ZA-Z][WD_]{5,19} |
User name: including English lowercase, Chinese characters, numbers, underscores, cannot be all numbers, underline cannot be at the end
| The code is as follows |
Copy Code |
|
/^[a-z0-9_u4e00-u9fa5]+[^_]$/g
Utf-8 under
Preg_match ("/^[a-z0-9_x80-xff]+[^_]$/g", $a);
GBK under:
Preg_match ("/^[a-z0-9_". Chr (0XA1). " -". Chr (0xFF)."] +[^_]$/", $a) |
Mailbox
| The code is as follows |
Copy Code |
|
<?php
function Is_email ($email) {
return strlen ($email) > 6 && preg_match ("/^[w-.] +@[w-]+ (. w+) +$/", $email);
}
?> |
URL address
| code is as follows |
copy code |
|
<?php
function Autolink ($foo)
{
$foo = Eregi_replace (((F|HT) { 1}tp://) [-a-za-z0-9@:%_/+.~#?&//=]+] ', ' <a href= '/1 "mce_href="/1 "Target=_blank rel=nofollow>/1</a > ', $foo);
if (Strpos ($foo, "http") = = FALSE) {
$foo = eregi_replace (' (www.[ -a-za-z0-9@:%_/+.~#?&//=]+) ', ' <a href= "HTTP:///1" mce_href= "HTTP:///1" Target=_blank rel=nofollow >/1< /a> ', $foo);
}else{
$foo = Eregi_replace ([[: Space:] () [{}]) (www.[ -a-za-z0-9@:%_/+.~#?&//=]+) ', '/1<a href= "HTTP:///2" mce_href= "HTTP:///2" Target=_blank Rel=nofollow </a> ', $foo);
}
Return $foo;
}
|