What are PHP regular expressions? How to use PHP regular expressions (with code)

不言
Release: 2023-04-03 11:40:01
Original
31637 people have browsed it

What is PHP regular expression? PHP regular expression is a grammatical rule that describes the structure of a string. It is a specific formatting pattern that can match, replace, and intercept matching strings. So, how to use PHP regular expression? Next let's take a look at specific examples.

What are PHP regular expressions? How to use PHP regular expressions (with code)

1. Introduction to regular expressions:

Regular expressions It is a grammatical rule used to describe character arrangement and matching patterns. It is mainly used for pattern segmentation, matching, search and replacement operations of strings.

1. Purpose: matching, search, replace, split

2. PHP provides two sets of regular expression function libraries

*1. Perl Compatible with regular expression functions (recommended) ​
​ 2. POSIX extended regular expression function

2. Syntax: ​

Expression The format of the expression: "/expression/[modifier]"

Explanation: "/" represents the delimiter of the regular expression, but it can also be other symbols: such as "#", "!"

Note: The delimiter cannot be letters, numbers and slashes\.

Like "#", "|", "!", etc.
Such as: /.../ #...# |....|

Among them Modifiers are optional and represent additional modifications to the expression.

3. Components of regular expressions:

1. Atoms are the basic units that make up regular expressions. When analyzing The regular expression should be used as a whole.

Including the following:

# & gt; single character, number, such as A-Z, A-Z, 0-9.​
            > Pattern units, such as (ABC) can be understood as large atoms composed of multiple atoms.
& gt; atomic table, such as [ABC].
                                                                                                                                                                                             . . Atomic table of characters

For example: [aoeiu] represents any vowel letter

[0-9] represents any digit

[a-z][0-9 ] represents two characters composed of lowercase letters and one digit. # Indicates that any character other than the atoms in square brackets is the negation of []. Any non-lowercase letter

{m} represents the control of the number of previous atoms, indicating m times

For example: [0-9]{4} means 4 is a number

                                                                                                                                                                   ,                           For example: [0-9]{2,} represents two or more digits

                                                                                                                                                                                                                    . Indicates the control of the number of the previous atoms, indicating any number of times, equivalent to {0,}  
                                                                                               .                    The number of atoms is controlled, indicating 0 or 1 time (optional). Equivalent to {0,1}

For example: positive integer: [1-9][0-9]* \-]?[0-9]  

                                                                                            using using using using ’             through ’ s ’ ’ s ’     through through using ’ s ’     through ’ through through through through ‐ ‐ ‐ ‐‐‐‐‐]? 
                                                                                                                                                                                                                                                                        Can also use ?: To reject substores. (?:*?)
For example: (red) string red
(rea | blue) string red or blue
(abc) {2} indicates two abc
| Meaning
(Rea | Blue) String Red or Blue
^is used at the beginning of the regular unit block, indicating that it must be used at the end of the specified
$ at the end of the block block, indicating that it must be based on the end of the block block, indicating that it must be based on the end of the block, which means that it must be based on the must be used. The specified end
. Represents any character other than a newline character
Commonly used combinations: .*? Represents a minimum match of all characters (rejecting greedy matching)


3. Ordinary escape Characters:


##\d matches a number; equivalent to [0-9]

\D matches any character except numbers; equivalent to [^0-9]##\w\W\s Matches a whitespace character; equivalent to [\f\n\r \t\v] \S Matches any character except whitespace characters; equivalent to [^\f\n\r\t \v]##\f matches a form feed and is equivalent to \x0c or \cL Matches a newline character; equivalent to \x0a or \cJ Matches a carriage return character equivalent to \ x0d or \cM matches a tab; equivalent to \x09\ or \cl Matches a vertical tab; equivalent to \x0b or \ck Matches an octal number Matches a hexadecimal numberMatch a control character
matches an English letter, Numbers or underscores; equivalent to [0-9a-zA-Z_]
matches any character except English letters, numbers and underscores; etc. Equivalent to [^0-9a-zA-Z_]


##\n
##\r
\t
\v

\oNN

\xNN
\cC

         4. 模式修整符    
        i 表示不区分大小写;    
            "/[a-zA-Z]/" "/[a-z]/i"    
        s 表示匹配视为单行(就是可以让点.支持换行)    
        U 表示拒绝贪婪匹配

四、 php正则表达式函数:    

    preg_grep --  返回与模式匹配的数组单元     

    * preg_match_all -- 进行全局正则表达式匹配 , 返回共计匹配的个数。    
        和下面的一样,不同的是匹配到最后(全局匹配)     

    * preg_match -- 进行正则表达式匹配,只匹配一次,返回1,否则0,    
        格式:preg_match("正则表达式","被匹配的字串",存放结果的变量名,PREG_OFFSET_CAPTURE,起始偏移量)    
        其中:PREG_OFFSET_CAPTURE表示获取匹配索引位置    
              起始偏移量:从指定位置开始匹配     

    preg_quote -- 转义正则表达式字符     

    preg_split -- 用正则表达式分割字符串     

    preg_replace -- 执行正则表达式的搜索和替换

实例:

1.php正则表达式匹配

//正则匹配函数preg_match()

//模糊匹配(包含形式)
//if(preg_match("/a/","qwertayuio")){ //匹配字串中是否包含a字符
//if(preg_match("/(abc)/","qwerta bcayuio")){ //匹配字串中是否包含abc字串
//if(preg_match("/[abc]/","qwertbycuiop")){ //匹配字串中是否包含a、b或c字字符
//if(preg_match("/[0-9]/","qwertbycuiop")){ //匹配字串中是否包数字
//if(preg_match("/[a-z]/","12345a6789")){ //匹配字串中是否包小写字母
//if(preg_match("/[0-9]{2}/","qwe89rqw9re8qwer",$a)){ //匹配字串中是否包两位的数字
//if(preg_match("/[0-9]{2,}/","qwe12rqw9re8qwer",$a)){ //匹配字串中是否包至少两位的数字
if(preg_match("/[0-9]{2,4}/","qwe12567rqw9re8qwer",$a)){ //匹配字串中是否包至少两位到4位的数字
    echo "匹配!";
}else{
    echo "不匹配!";
}

echo $a[0];   

echo "<hr/>";

//精确匹配
//if(preg_match("/^[0-9]{2}$/","34")){ //精确匹配两位数字
if(preg_match("/^[1][35][0-9]{9}$/","13520319616")){ //匹配一个手机号码
    echo "匹配!";
}else{
    echo "不匹配!";
}
Copy after login

What are PHP regular expressions? How to use PHP regular expressions (with code)

What are PHP regular expressions? How to use PHP regular expressions (with code)

2.php正则的元字符匹配

//正则的元字符使用
//检测是否是一个合法的mail地址
if(preg_match("/^[\w\.]+@[\w]+(\.[a-zA-Z]+){1,3}$/","asd@asd.com")){
    echo "正确";
}else{
    echo "错误";
}

/*
//检测是否是一个十六进制整数(正整数,负整数,0)
if(preg_match("/^-?0[xX][\da-fA-F]+$/","-2")){
    echo "正确";
}else{
    echo "错误";
}

//检测是否是一个整数(正整数,负整数,0)
//if(preg_match("/^-?[0-9]+$/","-2")){
if(preg_match("/^-?\d+$/","-2")){
    echo "正确";
}else{
    echo "错误";
}
*/

/*
//检测一个变量名是否正确
//if(preg_match("/^[a-zA-Z_][a-zA-Z0-9_]*$/","1a5b_c")){
if(preg_match("/^[a-zA-Z_][\w]*$/","a5b_c")){
    echo "正确";
}else{
    echo "错误";
}
*/

//匹配字串中的4位数字
//preg_match("/[0-9]{4}/","qweabi123srqcdwer456iabs7890asfcd",$a);
//preg_match("/\d{4}/","qweabi123srqcdwer456iabs7890asfcd",$a);
//var_dump($a[0]); //匹配:7890

//preg_match_all("/(ab|cd)/","qweabisrqcdweriabsasfcd",$a);
//var_dump($a[0]); //匹配字串所有ab或cd

//preg_match_all("/is/","qweisrqwerisasfd",$a);
//var_dump($a); //匹配字串所有is

//preg_match("/.*/","*a\nbc",$a);
//var_dump($a); //*a
Copy after login

3.php正则表达式匹配网页

//正则匹配函数:preg_match   preg_match_all
$str=<<<yfstr
    <div id="mainNav" class="clearfix">
        <a href="index.php">首页</a>
        <a href="category.php?id=3">GSM手机</a>
       <a href="category.php?id=4">双模手机</a>
       <a href="category.php?id=6">手机配件</a>
       <a href="group_buy.php">团购
       商品</a>
       <a href="activity.php">优惠活动</a>
       <a href="snatch.php">夺宝奇兵</a>
       <a href="auction.php">拍卖活动</a>
       <a href="exchange.php">积分商城</a>
       <a href="message.php">留言板</a>
       <a href="http://bbs.ecshop.com/">EC论坛</a>
 </div>
yfstr;

echo "<table width=&#39;900&#39; border=&#39;1&#39;>";
echo "<tr><th>名称</th><th>URL地址</th><th>链接</th></tr>";
//使用正则匹配
preg_match_all("/<a href=\"(.*?)\".*?>(.*?)<\/a>/s",$str,$a);
foreach($a[0] as $k=>$v){
    echo "<tr>";
    echo "<td>{$a[2][$k]}</td>";
    echo "<td>{$a[1][$k]}</td>";
    echo "<td>{$v}</td>";
    echo "</tr>";
}
echo "</table>";
Copy after login

注:使用<<< 这个是php定界符

使用格式:

<<<EOF...EOF;
Copy after login

使用定界符无需给双引号增加转义字符,可以参考如下:

$str=”/

class=\"clearfix\">/”;

4正则的其他函数使用

//正则的其他函数使用:
//preg_quote -- 转义正则表达式字符
//preg_split -- 用正则表达式分割字符串
//preg_replace -- 执行正则表达式的搜索和替换

//1.preg_quote -- 转义正则表达式字符
echo preg_quote("(abc){10}","&#39;");//在每个增则表达式语法的字符前增加一个反斜杠

$s = "a{4}";
preg_match("/".preg_quote($s)."/","werta{4}yu",$a);
var_dump($a);

echo "<br/>";
//2. preg_split -- 用正则表达式分割字符串
$s = "12,34:56;784;35,67:897:65";
$list = preg_split("/[,:;]/",$s);
var_dump($list);

echo "<hr/>";

//3. preg_replace执行正则表达式的搜索和替换
$s = "12,34:56;784;35,67:897:65";
//要求将上面的:,;都换成空格
echo preg_replace("/[,;:]/"," ",$s);

$str = "<ul style=&#39;color:red&#39;>
            <li>aaaaa</li>
            <li>bbbbb</li>
            <li>ddddd</li>
            <li>eeeee</li>
        </ul>";

//将上面字串中所有li标签中都添加一个b标签。
echo "<hr/>";
echo $str;

echo "<hr/>";
//echo preg_replace("/<li>(.*?)<\/li>/","<li><b>\\1</b></li>",$str);
//echo preg_replace("/<li>(.*?)<\/li>/","<li><b>\$1</b></li>",$str);
echo preg_replace("/<li>(.*?)<\/li>/",&#39;<li><b>$1</b></li>&#39;,$str);
Copy after login

5.子存储(扩展)

//子存储使用
$date="[2012-08-09],[2012,09-19],[2011/08,09],[2012/10/09],[2013,08,01]";

//将上面字串中合法的日期匹配出来
preg_match_all("/\[[0-9]{4}([\-,\/])[0-9]{2}\\1[0-9]{2}\]/",$date,$a);
var_dump($a);

echo "<hr/>";

$str = "<ul 
style=&#39;color:red&#39;><br/>
            <li>aaaaa</li>
            <li>bbbbb</li>
            <li>ddddd</li>
            <li>eeeee</li>
        </ul>";
//将上面字串中的html标记删除掉(替换空)
echo  preg_replace("/<\/?.*?\/?>/s","",$str);
Copy after login

附录

php常用正则表达式

^\d+$  \d 是代表0-9  $必须要以....结束  这是代表非负整数   正则表达式

平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: 
"^\d+$"  //非负整数(正整数 + 0) 
"^[0-9]*[1-9][0-9]*$"  //正整数 
"^((-\d+)|(0+))$"  //非正整数(负整数 + 0) 
"^-[0-9]*[1-9][0-9]*$"  //负整数 
"^-?\d+$"    //整数 
"^\d+(\.\d+)?$"  //非负浮点数(正浮点数 + 0) 
"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数 
"^((-\d+(\.\d+)?)|(0+(\.0+)?))$"  //非正浮点数(负浮点数 + 0) 
"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数 
"^(-?\d+)(\.\d+)?$"  //浮点数 
"^[A-Za-z]+$"  //由26个英文字母组成的字符串 
"^[A-Z]+$"  //由26个英文字母的大写组成的字符串 
"^[a-z]+$"  //由26个英文字母的小写组成的字符串 
"^[A-Za-z0-9]+$"  //由数字和26个英文字母组成的字符串 
"^\w+$"  //由数字、26个英文字母或者下划线组成的字符串 
"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"    //email地址 
"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$"  //url 
/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/ // 年-月-日 
/^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/ // 月/日/年 
"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$" //Emil 
/^((\+?[0-9]{2,4}\-[0-9]{3,4}\-)|([0-9]{3,4}\-))?([0-9]{7,8})(\-[0-9]+)?$/ //电话号码 
"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$" //IP地址 
匹配中文字符的正则表达式: [\u4e00-\u9fa5] 
匹配双字节字符(包括汉字在内):[^\x00-\xff] 
匹配空行的正则表达式:\n[\s| ]*\r 
匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/ 
匹配首尾空格的正则表达式:(^\s*)|(\s*$) 
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 
匹配网址URL的正则表达式:^[a-zA-z]+://([url=file://\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$]\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$[/url] 
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$ 
匹配国内电话号码:(\d{3}-|\d{4}-)?(\d{8}|\d{7})? 
匹配腾讯QQ号:^[1-9]*[1-9][0-9]*$ 

元字符及其在正则表达式上下文中的行为: 
\ 将下一个字符标记为一个特殊字符、或一个原义字符、或一个后向引用、或一个八进制转义符。 
^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的Multiline 属性,^ 也匹配 ’\n’ 或 ’\r’ 之后的位置。 
$ 匹配输入字符串的结束位置。如果设置了 RegExp 对象的Multiline 属性,$ 也匹配 ’\n’ 或 ’\r’ 之前的位置。 
* 匹配前面的子表达式零次或多次。 
+ 匹配前面的子表达式一次或多次。+ 等价于 {1,}。 
? 匹配前面的子表达式零次或一次。? 等价于 {0,1}。 
{n} n 是一个非负整数,匹配确定的n 次。 
{n,} n 是一个非负整数,至少匹配n 次。 
{n,m} m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。在逗号和两个数之间不能有空格。 
? 当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认 
的贪婪模式则尽可能多的匹配所搜索的字符串。 
. 匹配除 "\n" 之外的任何单个字符。要匹配包括 ’\n’ 在内的任何字符,请使用象 ’[.\n]’ 的模式。 
(pattern) 匹配pattern 并获取这一匹配。 
(?:pattern) 匹配pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。 
(?=pattern) 正向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。 
(?!pattern) 负向预查,与(?=pattern)作用相反 
x|y 匹配 x 或 y。 
[xyz] 字符集合。 
[^xyz] 负值字符集合。 
[a-z] 字符范围,匹配指定范围内的任意字符。 
[^a-z] 负值字符范围,匹配任何不在指定范围内的任意字符。 
\b 匹配一个单词边界,也就是指单词和空格间的位置。 
\B 匹配非单词边界。 
\cx 匹配由x指明的控制字符。 
\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 为十六进制转义值。十六进制转义值必须为确定的两个数字长。 
\num 匹配 num,其中num是一个正整数。对所获取的匹配的引用。 
\n 标识一个八进制转义值或一个后向引用。如果 \n 之前至少 n 个获取的子表达式,则 n 为后向引用。否则,如果 n 为八进制数字 (0-7),则 n 为一个 
八进制转义值。 
\nm 标识一个八进制转义值或一个后向引用。如果 \nm 之前至少有is preceded by at least nm 个获取得子表达式,则 nm 为后向引用。如果 \nm 之前至 
少有 n 个获取,则 n 为一个后跟文字 m 的后向引用。如果前面的条件都不满足,若 n 和 m 均为八进制数字 (0-7),则 \nm 将匹配八进制转义值 nm。 
\nml 如果 n 为八进制数字 (0-3),且 m 和 l 均为八进制数字 (0-7),则匹配八进制转义值 nml。 
\un 匹配 n,其中 n 是一个用四个十六进制数字表示的Unicode字符。 
匹配中文字符的正则表达式: [\x{4e00}-\x{9fa5}] 
匹配双字节字符(包括汉字在内):[^x00-xff] 
匹配空行的正则表达式:n[s| ]*r 
匹配HTML标记的正则表达式:/<(.*)>.*|<(.*) />/ 
匹配首尾空格的正则表达式:(^s*)|(s*$) 
匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)* 
匹配网址URL的正则表达式:[url=http://([w-]+.)+[w-]+(/[w]http://([w-]+.)+[w-]+(/[w[/url]- ./?%&=]*)? 
利用正则表达式限制网页表单里的文本框输入内容: 
用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,&#39;&#39;)" 
用正则表达式限制只能输入全角字符: 
用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^d]/g,&#39;&#39;) "onbeforepaste="clipboardData.setData 
(&#39;text&#39;,clipboardData.getData(&#39;text&#39;).replace(/[^d]/g,&#39;&#39;))" 
用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[W]/g,&#39;&#39;) "onbeforepaste="clipboardData.setData 
(&#39;text&#39;,clipboardData.getData(&#39;text&#39;).replace(/[^d]/g,&#39;&#39;))" 
=========常用正则式 

匹配中文字符的正则表达式: [\x{4e00}-\x{9fa5}]
匹配双字节字符(包括汉字在内):[^\x00-\xff] 
匹配空行的正则表达式:\n[\s| ]*\r 
匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/ 
匹配首尾空格的正则表达式:(^\s*)|(\s*$) 
匹配IP地址的正则表达式:/(\d+)\.(\d+)\.(\d+)\.(\d+)/g // 
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 
匹配网址URL的正则表达式:[url=http://(/[\w-]+\.)+[\w-]+(/[\w]http://(/[\w-]+\.)+[\w-]+(/[\w[/url]- ./?%&=]*)? 
sql语句:^(select|drop|delete|create|update|insert).*$ 
1、非负整数:^\d+$ 
2、正整数:^[0-9]*[1-9][0-9]*$ 
3、非正整数:^((-\d+)|(0+))$ 
4、负整数:^-[0-9]*[1-9][0-9]*$ 
5、整数:^-?\d+$ 
6、非负浮点数:^\d+(\.\d+)?$ 
7、正浮点数:^((0-9)+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ 
8、非正浮点数:^((-\d+\.\d+)?)|(0+(\.0+)?))$ 
9、负浮点数:^(-((正浮点数正则式)))$ 
10、英文字符串:^[A-Za-z]+$ 
11、英文大写串:^[A-Z]+$ 
12、英文小写串:^[a-z]+$ 
13、英文字符数字串:^[A-Za-z0-9]+$ 
14、英数字加下划线串:^\w+$ 
15、E-mail地址:^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$ 
16、URL:^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\s*)?$ 
或:^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\&#39;:+!]*([^<>\"\"])*$ 
17、邮政编码:^[1-9]\d{5}$ 
18、中文:^[\u0391-\uFFE5]+$ 
19、电话号码:^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$ 
20、手机号码:^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$ 
21、双字节字符(包括汉字在内):^\x00-\xff 
22、匹配首尾空格:(^\s*)|(\s*$)(像vbscript那样的trim函数) 
23、匹配HTML标记:<(.*)>.*<\/\1>|<(.*) \/> 
24、匹配空行:\n[\s| ]*\r 
25、提取信息中的网络链接:(h|H)(r|R)(e|E)(f|F) *= *(&#39;|")?(\w|\\|\/|\.)+(&#39;|"| *|>)? 
26、提取信息中的邮件地址:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 
27、提取信息中的图片链接:(s|S)(r|R)(c|C) *= *(&#39;|")?(\w|\\|\/|\.)+(&#39;|"| *|>)? 
28、提取信息中的IP地址:(\d+)\.(\d+)\.(\d+)\.(\d+) 
29、提取信息中的中国手机号码:(86)*0*13\d{9} 
30、提取信息中的中国固定电话号码:(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8} 
31、提取信息中的中国电话号码(包括移动和固定电话):(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14} 
32、提取信息中的中国邮政编码:[1-9]{1}(\d+){5} 
33、提取信息中的浮点数(即小数):(-?\d*)\.?\d+ 
34、提取信息中的任何数字 :(-?\d*)(\.\d+)? 
35、IP:(\d+)\.(\d+)\.(\d+)\.(\d+) 
36、电话区号:/^0\d{2,3}$/ 
37、腾讯QQ号:^[1-9]*[1-9][0-9]*$ 
38、帐号(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$ 
39、中文、英文、数字及下划线:^[\u4e00-\u9fa5_a-zA-Z0-9]+$
Copy after login

 相关推荐:

PHP最常用的正则表达式的详解

PHP正则表达式合集

The above is the detailed content of What are PHP regular expressions? How to use PHP regular expressions (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!