Perl-compatible regular expressions
First, Function:
Preg_match (): Indicates whether the pattern matches the string and returns 1 if a match is found, otherwise 0
Preg_match_all (): Found all matches
Preg_replace (): Implementing the Find and Replace function
Second, the simple mode:
1. Literal: is a value that is written in exactly the same way as it is interpreted. For example, pattern a will match the letter A,ab will match AB and so on.
Delimiter: can be any character except alphanumeric or backslash, you must use the same character to mark the beginning and end of the pattern. Usually use a forward slash (/)
Metacharacters:
Quantifiers:
Common character Categories:
Third, Practice:
1. Single Match--pcre.php
<htmllang="en"><head><metacharset="UTF-8"><title>Testing PCRE
title
>
Head
><body>
# Script 14.1-pcre.phpif($_server[' Request_method '] = = ' POST ' { $pattern = Trim ($_post[' pattern ']); $subject = Trim ($_post[' subject ']); Echo "The result of checking
$pattern
Aganist
$subject
Is
"
; if (Preg_match ($pattern, $subject)) { echo' true!'; }Else{ echo' false!
'; }} ?>
<formAction="pcre.php"method="POST"><p>Regular Expression Pattern:<inputtype="text"name="pattern"value= "
"size="Max">(Include the delimiters)
P
><p>Test Subject:<inputtype="text"name="Subject"value ="
"size=" /> "
P
><inputtype="Submit"name= "submit"value="test!" >
Form
>
Body
>
HTML
>
--e-mail verification: E-mail starts with letters, numbers or underscores, and a period (.) or dash.
2. All matching--matches.php
<htmllang="en"><head><metacharset="UTF-8"><title>Testing PCRE
title
>
Head
><body>
# Script 14.1-pcre.php if ($_server[' Request_method '] = = ' POST ') { $pattern = Trim ($_post[' pattern ')]; $subject = Trim ($_post[' subject ']); Echo "The result of checking
$pattern
Aganist
$subject
Is
"; if (Preg_match_all ($pattern, $subject, $matches)) { echo' true!'; Echo '
'. Print_r ($matches,1).''; }Else{Echo' false!'
; } }?><formAction="matches.php"method="POST"><p>Regular Expression Pattern:<inputtype="text"name="pattern"value ="
"size="> "(Include the delimiters)
P
><p>Test Subject:<textareaname="Subject"rows="5"cols= "> "
if(isset($subject)) echo htmlentities ($subject); ?>
textarea
>
P
><inputtype="Submit"name="Submit"value ="test!" >
Form
>
Body
>
HTML
>
3. Match and replace--replace.php
<htmllang="en"><head><metacharset="UTF-8"><title>Testing PCRE
title
>
Head
><body>
# Script 14.1-pcre.phpif($_server[' Request_method '] ==' POST ') {$pattern= Trim ($_post[' pattern ']);$subject= Trim ($_post[' Subject ']);$replace= Trim ($_post[' Replace ']);Echo$subject;Echo"The result of replacing
$pattern
With
$replace
Inch
$subject
"; if (Preg_match ($pattern, $subject)) { echo preg_replace ($pattern, $replace, $subject). '
'; }Else{Echo' The pattern is not found!'
; } }?><formAction="replace.php"method="POST"> <p>Regular Expression Pattern:<inputtype="text"name="pattern"value="
" Size="Max">(Include the delimiters)
P
><p>Replacement:<inputtype="text"name="Replace"value ="
"size="> "
P
><p>Test Subject:<textareaname="Subject"rows="5"cols="Max" >
if(isset($subject)) echo htmlentities ($subject ); ?>
textarea
>
P
><inputtype="Submit"name="Submit"value ="test!" >
Form
>
Body
>
HTML
>
'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ (''). Text (i)); }; $numbering. FadeIn (1700); }); }); The above describes the PHP learning practiced hand (16), including aspects of the content, I hope the PHP tutorial interested in a friend helpful.