Regular expression (Regular Expression)
Regular expression system:
1.POSIX
2.Perl
The regex used in PHP is pcre:
Note:pcre (Perl-compatible regular expressions, Perl compatible Regular Expressions)
Pcre Syntax:
1. Delimiters
must appear in pairs and can use any character other than 0-9a-za-z
2. Atomic
1. Both visible and invisible characters that need to be matched are atoms.
2. A regular expression contains at least one atom
3. When you need to match symbols such as "(", "[", "^" and so on that contain semantics, you need to escape with the backslash.
Atomic characters:
F Matching page breaks
N Match line break
R Matching Carriage return character
T-Match tab
V Matching Vertical Tabs
3. Meta character
Escape character
^ Match string at start
$ Match String End
. Matches any single character except "N"
* Match the preceding subexpression 0 or more times
+ Match previous subexpression 1 or more times
? Match the preceding subexpression 0 or 1 times
{n} matches n times
{n,} matches n times or more n times
{n,m} minimum match n times up to M times, (n<=m)
[] brackets represent atomic tables, and the atoms in the middle are equal in status. Matches any one of the characters in the table when it matches
[^] Caron, excluding the characters that are contained in the following atomic table.
(pattern) matches the pattern and gets the match.
Num matches a reference to the obtained Num.
(?:p Attern) match pattern but do not get this match
(? =pattern) positive forward check, non-fetch match, for example: Windows (? =xp|7) matches windows in Windows mismatch in Windows98
(?! =pattern) positive negation to check for non-fetch matches, for example: Windows (?! 98|2000), can match windows in XP, cannot match windows in Windows98
(? <=pattern) Reverse positive check, not get match. For example: (? <=my| postgre) SQL can match the SQL in MySQL and cannot match the SQL in MSSQL
(? <!pattern) reverse negation of the pre-search, not to obtain a match. For example: (? <! my| postgre) SQL matches the SQL in MSSQL and does not match the SQL in MySQL
B Match Word boundary
B matches a character other than a word boundary
D matches any one number. equivalent to [0-9]
D matches any character other than a digit. equivalent to [^0-9]
s matches any white space character (including spaces, tabs, page breaks, and so on). equivalent to [FNRTV]
S matches any non-white-space character. equivalent to [^FNRTV]
W matches any number, letter, or underline. equivalent to [0-9a-za-z]
W matches any character that is not numeric, alphanumeric, or underlined. equivalent to [^0-9a-za-z]
4. Pattern modifier
I do not case sensitive
M in this mode if there is a carriage return or line break, ^ and $ will match the beginning and end of each line
s let. can match n
x Ignore whitespace
U cancel Greed, equal to (. *?)
A is the same as ^ effect
D at the end without ignoring the carriage return, at the end of the $ character at the time, after the matching string plus carriage return, $ can still match it successfully. But plus d, the end of the carriage return, no longer match
Note: Regular expressions are matched from left to right
Regular expressions that are commonly used
1, nonnegative integer: "^d+$"
2, positive integer: "^[0-9]*[1-9][0-9]*$"
3, non-positive integer: "^ (-d+) | ( 0+)) $ "
4, Negative integer: "^-[0-9]*[1-9][0-9]*$"
5, Integer: "^-?d+$"
6, nonnegative floating-point number: "^d+ (. d+)? $"
7, positive floating-point number: "^ ((0-9) +.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*. [0-9]+) | ([0-9]*[1-9][0-9]*)] $
8, non-positive floating-point number: "^ ((-d+.d+)?) | (0+ (. 0+)?)) $
9, negative floating point number: "^ (-((positive float)) $"
10. English string: "^[a-za-z]+$"
11, English capital string: "^[a-z]+$"
12, English lowercase string: "^[a-z]+$"
13, English character number string: "^[a-za-z0-9]+$"
14, the English numeral adds the underline string: "^w+$"
15, e-mail address: "^[w-]+ (. [ w-]+) *@[w-]+ (. [ w-]+) +$ "
16, URL: "^[a-za-z]+://(w+ (-w+) *) ( w+ (-w+) *)) * (? s*)? $ "
The following function, written in PHP, can get all the link addresses in any string $string ($string can be read directly from an HTML page file), and the result is returned in an array. The function automatically excludes e-mail addresses, And there are no duplicate elements in the returned array.
The code is as follows |
Copy Code |
function Getalllink ($string) { $string = Str_replace ("R", "", $string); $string = Str_replace ("n", "", $string); $regex [url] = "((http|https|ftp|telnet|news)://)? ([a-z0-9_-/.] +. [][a-z0-9:;&#@=_~%?/.,+-]+) "; $regex [email] = ([a-z0-9_-]+) @ ([a-z0-9_-]+.[ a-z0-9-._-]+) "; Remove the text between labels $string = Eregi_replace (">[^<>]+<", "><", $string); Remove JavaScript code $string = Eregi_replace ("<!--. *//-->", "", $string); |
Remove HTML tags that are not <a>
code is as follows |
copy code |
$string = Eregi_replace ("<[^a][^<>]*>", "" ", $string); //Remove Email link $string = eregi_replace ("<a" ([]+) href= (["']*) mailto: ($regex [email]) ([" ']*] [^>]*> "," ", $ string); //Replace required page links $string = eregi_replace ("<a" ([]+) href= (["']*") ($regex [url]) ([']* ' [^>]*>], "\3t", $string ); $output [0] = Strtok ($string, "T"); while ($temp = Strtok ("T")) { if ($temp &&!in_array ($temp, $output) $output [+ + $i] = $temp; br>} Return $output; } |
Here's a sample written in PHP syntax
Verify that the string contains only numbers and English, string length and between 4~16 characters
The code is as follows |
Copy Code |
<?php $str = ' a1234 '; if (Preg_match ("^[a-za-z0-9]{4,16}$", $str)) { echo "Validation succeeded";} else { echo "Validation lost?? quot;;}? > |
Simple identification of the size of Taiwan's identity card
The code is as follows |
Copy Code |
<?php $str = ' a1234 '; if (Preg_match ("^ (?:d {15}|d{18}) $", $str)) { echo "Validation succeeded"; } else { echo "Validation lost?? quot;;} ?> |
The following code implements a block of code in the text, like the code you see in the script house.
The code is as follows |
Copy Code |
function Codedisp ($code) { Global $discuzcodes; $discuzcodes [' Pcodecount ']++; $code = Htmlspecialchars (str_replace (' \ ", '" '), Preg_replace ("/^[nr]* (. +?) [nr]*$/is "," \1 ", $code))); $discuzcodes [' codehtml '] [$discuzcodes [' pcodecount ']] = "<br><div class=" Msgheader "><div class=" Right "><a href=" ### "class=" Smalltxt "onclick=" Copycode ($ (' phpcode$discuzcodes[codecount] '); >[Copy This Code]</a></div> code as follows: </div><div class= "Msgborder" id= "Phpcode$discuzcodes[codecount" > ". FHTML2 ($code)." </div><br> "; $discuzcodes [' Codecount ']++; return "[Tdiscuz_code_$discuzcodes[pcodecount]t]"; } $message = Preg_replace ("/s*[code]" (. +?) [/code]s*/ies "," Codedisp (' \1 ') ", $message); $message = Preg_replace ("/s*[html]" (. +?) [/html]s*/ies "," Htmldisp (' \1 ') ", $message); |