1.正则表达式基础知识
含义:由普通字符和(a-z)和一些特殊字符组成的字符串模式
功能:有效性验证。
替换文本。
从一个字符串提取一个子字符串。
分类:POSIX和Perl
POSIX风格更容易掌握,但不能用于二进制模式,而perl相对比较复杂。
2.POSIX风格的正则表达式
1.编写正则表达式
表4.3 POSIX正则表达式语法格式列表
字 符
|
描 述
|
\
|
转义字符,用于转义特殊字符。例如,'.'匹配单个字符,'\.'匹配一个点号。'\-'匹配连字符'-','\\'匹配符号'\'
|
^
|
匹配输入字符串的开始位置。例如'^he'表示以'he'开头的字符串
|
$
|
匹配输入字符串的结束位置。例如,'ok$'表示以'ok'结尾的字符串
|
*
|
匹配前面的子表达式零次或多次。例如,'zo*'能匹配"z"以及"zoo"。*等价于{0,}
|
+
|
匹配前面的子表达式一次或多次。例如,'zo+'能匹配"zo"以及"zoo",但不能匹配"z"。+等价于{1,}
|
?
|
匹配前面的子表达式零次或一次。例如,'do(es)?'可以匹配"do"或"does"中的"do"。'?'等价于{0,1}
|
{n}
|
n是一个非负整数。匹配确定的n次。例如,'o{2}'不能匹配"Bob"中的'o',但是能匹配"food" 中的两个'o'
|
{n,}
|
n是一个非负整数。至少匹配n次。例如,'o{2,}'不能匹配"Bob"中的'o',但能匹配"foooood" 中的所有'o'。'o{1,}'等价于'o+'。'o{0,}'则等价于'o*'
|
{n,m}
|
m和n均为非负整数,其中n≤m。最少匹配n次且最多匹配m次。例如,"o{1,3}"将匹配"fooooood"中的前三个'o'。'o{0,1}'等价于'o?'。请注意在逗号和两个数之间不能有空格
|
?
|
当该字符紧跟在任何一个其他限制符(*, +, ?, {n}, {n,}, {n,m})后面时,匹配模式是非贪婪的。非贪婪模式尽可能少地匹配所搜索的字符串,而默认的贪婪模式则尽可能多地匹配所搜索的字符串。例如,对于字符串"oooo",'o+?'将匹配单个"o",而'o+' 将匹配所有'o'
|
.
|
匹配除"\n"之外的任何单个字符,要匹配包括'\n' 在内的任何字符,可以使用'[.\n]'的模式
|
(pattern)
|
匹配pattern并获取这一匹配。所获取的匹配保存到相应的数组中。要匹配圆括号字符,请使用 '\(' 或 '\)'
|
(?:pattern)
|
匹配pattern但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储。这在使用"或"|"来组合一个模式的各个部分时很有用。例如,'industr(?:y|ies).就是一个比'industry|industries'更简略的表达式
|
(?=pattern)
|
正向预查,在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如,'Windows(?=95|98|NT|2000)'能匹配"Windows 2000"中的"Windows",但不能匹配"Windows 3.1"中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始
|
(?!pattern)
|
负向预查,在任何不匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如'Windows(?!95|98|NT|2000)'能匹配"'Windows 3.1"中的"Windows",但不能匹配"Windows 2000"中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始
|
x|y
|
匹配x或y。例如,'z|food' 能匹配"z"或"food",'(z|f)ood'则匹配"zood"或"food"
|
[xyz]
|
字符集合。匹配所包含的任意一个字符。例如,'[abc]'可以匹配"plain"中的'a'
|
[^xyz]
|
负值字符集合。匹配未包含的任意字符。例如,'[^abc]'可以匹配"plain"中的'p'
|
[a-z]
|
字符范围。匹配指定范围内的任意字符。例如,'[a-z]'可以匹配'a'到'z' 范围内的任意小写字母字符
|
[^a-z]
|
负值字符范围。匹配不在指定范围内的任意字符。例如,'[^a-z]'可以匹配不在'a' 到'z'范围内的任意字符
|
The following are several examples of simple regular expressions:
●'[A-Za-z0-9] ': represents all uppercase letters, lowercase letters and numbers from 0 to 9.
●'^hello': Represents a string starting with hello.
●'world$': represents a string ending with world.
●'.at': Represents a string starting with any single character except "n" and ending with "at", such as "cat", "nat", etc.
●'^[a-zA-Z]': Represents a string starting with a letter.
●'hi{2}': Indicates the letter h followed by two i's, i.e. hii.
●'(go)+': Indicates a string containing at least one 'go' string, such as 'gogo'
ID card numbers generally consist of 18 digits or 17 digits followed by an X or Y letter To match the ID number, you can write:
^[0-9]{17}([0-9]|X|Y)$
The regular expression of the email address can be written:
^[a-zA-Z0-9-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$
2. String matching
ereg() and eregi() functions
Use the ereg() function to find when a string matches a substring, and return the length of the matching string. You can also return an array of matching characters with the help of parameters. The syntax format is as follows:
int ereg(string ($pattern) , string $string [, array $regs ])
Copy code The code is as follows:
/*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]" . "
"; //Output "09.08.1988"
echo $regs[0] ."
"; //Output "1988-08-09"
echo $len; //Output 10
}
else
{
echo "Wrong date format: $date";
}
?>
3 .String replacement
ereg_replace() function syntax format is as follows:
string ereg_replace(string $pattern, string $replacement, string $string)
Description: The function uses string $replacement to replace string $string The part that matches $pattern and returns the replaced string. If no match is found,
Copy code The code is as follows:
$str ="hello world";
echo ereg_replace('[aeo]', 'x',$str). "
"; //Output 'hxllx wxrld'
$res='
hello';
echo ereg_replace('hello', $res,$str); //Use hyperlink to replace 'hello'
?>
4.分割数组
使用split()函数可以完成与explode()函数一样的功能,而且可以根据给出的正则表达式来分割字符串,并返回一个数组。语法格式如下:
array split(string $pattern , string $string [, int $limit ])
5.产生正则表达式
3.Perl兼容的正则表达式
1.编写正则表达式
表4.4 Perl兼容正则表达式扩充的语法格式
字 符
|
描 述
|
\b
|
匹配一个单词边界,也就是指单词和空格间的位置。例如,'er\b'可以匹配"never"中的 'er',但不能匹配"verb"中的'er'
|
\B
|
匹配非单词边界。'er\B'能匹配"verb"中的'er',但不能匹配"never"中的'er'
|
\cx
|
匹配由x指明的控制字符。例如,'\cM'匹配一个Control-M或回车符。x的值必须为A~Z或a~z之一。否则,将'c'视为一个原义的'c'字符
|
\d
|
匹配一个数字字符。等价于'[0-9]'
|
\D
|
匹配一个非数字字符。等价于'[^0-9]'
|
\f
|
匹配一个换页符。等价于'\x0c'和'\cL'
|
\n
|
匹配一个换行符。等价于'\x0a'和'\cJ'
|
\r
|
匹配一个回车符。等价于'\x0d'和'\cM'
|
\s
|
匹配任何空白字符,包括空格、制表符、换页符等。等价于' [ \f\n\r\t\v] '
|
\S
|
匹配任何非空白字符。等价于' [^ \f\n\r\t\v] '
|
\t
|
匹配一个制表符。等价于'\x09'和'\cI'
|
\v
|
匹配一个垂直制表符。等价于'\x0b'和'\cK'
|
\w
|
匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'
|
\W
|
匹配任何非单词字符,等价于'[^A-Za-z0-9_]'
|
\xn
|
匹配n,其中n为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如,'\x41' 匹配"A"。'\x041'则等价于'\x04' & "1"。正则表达式中可以使用ASCII编码
|
\num
|
匹配num,其中num是一个正整数。对所获取的匹配的引用。例如,'(.)\1'匹配两个连续的相同字符
|
\n
|
标志一个八进制转义值或一个后向引用。如果\n之前至少有n个获取得子表达式,则n为后向引用。否则,如果n为八进制数字(0~7),则n为一个八进制转义值
|
\nm
|
标志一个八进制转义值或一个后向引用。如果\nm之前至少有nm个获取得子表达式,则 nm为后向引用。如果\nm之前至少有n个获取,则n为一个后跟文字m的后向引用。如果前面的条件都不满足,若 n和m均为八进制数字(0~7),则\nm将匹配八进制转义值nm
|
\nml
|
如果n为八进制数字(0~3),且m和l均为八进制数字(0~7),则匹配八进制转义值nml
|
\un
|
匹配n,其中n是用4个十六进制数字表示的Unicode字符。例如,'\u00A9'匹配版权符号(©)
|
2. String matching
preg_match() function performs string search. The syntax format is as follows:
int preg_match(string $pattern, string $subject [, array $matches [, int $flags ]])
Description: The structure of this function is similar to the ereg() function, which searches the $subject string for content that matches the regular expression given by $pattern.
The preg_match() function returns the number of times $pattern is matched. Either 0 times (no match) or 1 time, because the preg_match() function will stop searching after the first match
There is also preg_match_all(), which continues to search from the end of the first match until the search is completed. the entire string.
The value of the preg_match_all() function parameter $flags can take the following three values:
●PREG_PATTERN_ORDER. The default item means that $matches[0] is an array of all pattern matches,
$matches[1] is an array of strings matched by the subpattern in the first bracket, and so on.
●PREG_SET_ORDER. If this flag is set, $matches[0] is the array of the first set of matches, $matches[1] is the array of the second set of matches, and so on.
●PREG_OFFSET_CAPTURE. PREG_OFFSET_CAPTURE can be used in combination with the other two tags.
If this tag is set, the associated string offset will also be returned for each matching result that occurs.
3. String replacement
Using the preg_replace() function can accomplish the same function as the function ereg_replace(), find matching substrings in the string, and replace the substring with the specified string.
The syntax format is as follows:
mixed preg_replace(mixed $pattern, mixed $replacement, mixed $subject [, int $limit])
4. String splitting
preg_split() function can use regular expressions The expression is used as a boundary to split a string, and the substring is stored in an array and returned. The function is similar to the split() function.
The syntax format is as follows:
array preg_split(string $pattern, string $subject [, int $limit [, int $flags ]])
Description: This function is case-sensitive and returns an array containing Substrings in $subject split along boundaries that match $pattern.
$limit is an optional parameter. If specified, up to $limit strings will be returned. If omitted or -1, there is no limit.
The value of $flags can be the following three:
●PREG_SPLIT_NO_EMPTY. If this flag is set, the function only returns a non-empty string.
●PREG_SPLIT_DELIM_CAPTURE. If this flag is set, matches of bracket expressions in delimiter patterns are also captured and returned.
PREG_SPLIT_OFFSET_CAPTURE. If this flag is set, the associated string offset will also be returned for each occurrence of a match.
4.3 Example - Verify form content
[Example 4.4] Use regular expressions to verify whether the form content entered by the user meets the format requirements.
Create a new EX4_4_Hpage.php file and enter the following code.
Copy code The code is as follows:
include 'EX4_4_Hpage.php'; //Include files EX4_4Hpage.php
$id=$_POST['ID'];
$pwd=$_POST['PWD'];
$phone=$_POST['PHONE'];
$Email =$_POST['EMAIL'];
$checkid=preg_match('/^w{1,10}$/',$id); //Check whether the string is within 10 characters
$checkpwd =preg_match('/^d{4,14}$/',$pwd); //Check whether it is between 4 and 14 numbers
$checkphone=preg_match('/^1d{10}$/' ,$phone); //Check whether it is an 11-digit number starting with 1
//Check the validity of the email address
$checkEmail=preg_match('/^[a-zA-Z0-9_-]+ @[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/',$Email);
if($checkid&&$checkpwd&&$checkphone&&$checkEmail) //If If both are 1, the registration is successful
echo "Registration successful!";
else
echo "Registration failed, wrong format";
?>
新建EX4_4_Ppage.php文件,输入以下代码:
2. String matching
preg_match() function performs string search. The syntax format is as follows:
int preg_match(string $pattern, string $subject [, array $matches [, int $flags ]])
Description: The structure of this function is similar to the ereg() function, which searches the $subject string for content that matches the regular expression given by $pattern.
The preg_match() function returns the number of times $pattern is matched. Either 0 times (no match) or 1 time, because the preg_match() function will stop searching after the first match
There is also preg_match_all(), which continues to search from the end of the first match until the search is completed. the entire string.
The value of the preg_match_all() function parameter $flags can take the following three values:
●PREG_PATTERN_ORDER. The default item means that $matches[0] is an array of all pattern matches,
$matches[1] is an array of strings matched by the subpattern in the first bracket, and so on.
●PREG_SET_ORDER. If this flag is set, $matches[0] is the array of the first set of matches, $matches[1] is the array of the second set of matches, and so on.
●PREG_OFFSET_CAPTURE. PREG_OFFSET_CAPTURE can be used in combination with the other two tags.
If this tag is set, the associated string offset will also be returned for each matching result that occurs.
3. String replacement
Using the preg_replace() function can accomplish the same function as the function ereg_replace(), find matching substrings in the string, and replace the substring with the specified string.
The syntax format is as follows:
mixed preg_replace(mixed $pattern, mixed $replacement, mixed $subject [, int $limit])
4. String splitting
preg_split() function can use regular expressions The expression is used as a boundary to split a string, and the substring is stored in an array and returned. The function is similar to the split() function.
The syntax format is as follows:
array preg_split(string $pattern, string $subject [, int $limit [, int $flags ]])
Description: This function is case-sensitive and returns an array containing Substrings in $subject split along boundaries that match $pattern.
$limit is an optional parameter. If specified, up to $limit strings will be returned. If omitted or -1, there is no limit.
The value of $flags can be the following three:
●PREG_SPLIT_NO_EMPTY. If this flag is set, the function only returns a non-empty string.
●PREG_SPLIT_DELIM_CAPTURE. If this flag is set, matches of bracket expressions in delimiter patterns are also captured and returned.
PREG_SPLIT_OFFSET_CAPTURE. If this flag is set, the associated string offset will also be returned for each occurrence of a match.
4.3 Example - Verify form content
[Example 4.4] Use regular expressions to verify whether the form content entered by the user meets the format requirements.
Create a new EX4_4_Hpage.php file and enter the following code.
Copy code The code is as follows:
include 'EX4_4_Hpage.php'; //Include files EX4_4Hpage.php
$id=$_POST['ID'];
$pwd=$_POST['PWD'];
$phone=$_POST['PHONE'];
$Email =$_POST['EMAIL'];
$checkid=preg_match('/^w{1,10}$/',$id); //Check whether the string is within 10 characters
$checkpwd =preg_match('/^d{4,14}$/',$pwd); //Check whether it is between 4 and 14 numbers
$checkphone=preg_match('/^1d{10}$/' ,$phone); //Check whether it is an 11-digit number starting with 1
//Check the validity of the email address
$checkEmail=preg_match('/^[a-zA-Z0-9_-]+ @[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/',$Email);
if($checkid&&$checkpwd&&$checkphone&&$checkEmail) //If If both are 1, the registration is successful
echo "Registration successful!";
else
echo "Registration failed, wrong format";
?>
新建EX4_4_Ppage.php文件,输入以下代码:
复制代码 代码如下:
include 'EX4_4_Hpage.php'; //包含文件EX4_4Hpage.php
$id=$_POST['ID'];
$pwd=$_POST['PWD'];
$phone=$_POST['PHONE'];
$Email=$_POST['EMAIL'];
$checkid=preg_match('/^\w{1,10}$/',$id); //检查字符串是否在10个字符以内
$checkpwd=preg_match('/^\d{4,14}$/',$pwd); //检查是否在4-14个字符之间
$checkphone=preg_match('/^1\d{10}$/',$phone); //检查是否是以1开头的11位数子
//检查Email地址的合法性
$checkEmail=preg_match('/^[a-zA-Z0-9_\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/',$Email);
if($checkid&&$checkpwd&&$checkphone&&$checkEmail) //如果都为1,则注册成功
echo "注册成功!";
else
echo "注册失败,格式不对";
?>
http://www.bkjia.com/PHPjc/323889.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323889.htmlTechArticle1. Basic knowledge of regular expressions meaning: a string composed of ordinary characters and (a-z) and some special characters Mode function: validity verification. Replacement text. Extract a...
from a string