什么叫正则表达式
正则表达式是对字符串进行操作的一种逻辑公式,就是用一些特定的字符组合成一个规则字符串,称之为正则匹配模式。$p = '/apple/'; $str = "apple banna"; if (preg_match($p, $str)) { echo 'matched'; }其中字符串'/apple/'就是一个正则表达式,他用来匹配源字符串中是否存在apple字符串。
PHP中使用PCRE库函数进行正则匹配,比如上例中的preg_match用于执行一个正则匹配,常用来判断一类字符模式是否存在。
正则表达式的基本语法
PCRE库函数中,正则匹配模式使用分隔符与元字符组成,分隔符可以是非数字、非反斜线、非空格的任意字符。经常使用的分隔符是正斜线(/)、hash符号(#) 以及取反符号(~),例如:/foo bar/ #^[^0-9]$# ~php~如果模式中包含分隔符,则分隔符需要使用反斜杠(\)进行转义。
/http:\/\//如果模式中包含较多的分割字符,建议更换其他的字符作为分隔符,也可以采用preg_quote进行转义。
$p = 'http://'; $p = '/'.preg_quote($p, '/').'/'; echo $p;分隔符后面可以使用模式修饰符,模式修饰符包括:i, m, s, x等,例如使用i修饰符可以忽略大小写匹配:
$str = "Http://www.imooc.com/"; if (preg_match('/http/i', $str)) { echo '匹配成功'; }
元字符与转义
正则表达式中具有特殊含义的字符称之为元字符,常用的元字符有:<span size="4px">\ 一般用于转义字符 ^ 断言目标的开始位置(或在多行模式下是行首) $ 断言目标的结束位置(或在多行模式下是行尾) . 匹配除换行符外的任何字符(默认) [ 开始字符类定义 ] 结束字符类定义 | 开始一个可选分支 ( 子组的开始标记 ) 子组的结束标记 ? 作为量词,表示 0 次或 1 次匹配。位于量词后面用于改变量词的贪婪特性。 (查阅量词) * 量词,0 次或多次匹配 + 量词,1 次或多次匹配 { 自定义量词开始标记 } 自定义量词结束标记</span><span>//下面的\s匹配任意的空白符,包括空格,制表符,换行符。[^\s]代表非空白符。[^\s]+表示一次或多次匹配非空白符。 $p = '/^我[^\s]+(苹果|香蕉)$/'; $str = "我喜欢吃苹果"; if (preg_match($p, $str)) { echo '匹配成功'; }</span><span size="4px">元字符具有两种使用场景,一种是可以在任何地方都能使用,另一种是只能在方括号内使用,在方括号内使用的有: \ 转义字符 ^ 仅在作为第一个字符(方括号内)时,表明字符类取反 - 标记字符范围 其中^在反括号外面,表示断言目标的开始位置,但在方括号内部则代表字符类取反,方括号内的减号-可以标记字符范围,例如0-9表示0到9之间的所有数字。</span><span>//下面的\w匹配字母或数字或下划线。 $p = '/[\w\.\-]+@[a-z0-9\-]+\.(com|cn)/'; $str = "我的邮箱是marchalex@163.com"; preg_match($p, $str, $match); echo $match[0];</span>
贪婪模式与懒惰模式
正则表达式中每个元字符匹配一个字符,当使用+之后将会变的贪婪,它将匹配尽可能多的字符,但使用问号?字符时,它将尽可能少的匹配字符,既是懒惰模式。贪婪模式:在可匹配与可不匹配的时候,优先匹配
//下面的\d表示匹配数字 $p = '/\d+\-\d+/'; $str = "我的电话是010-12345678"; preg_match($p, $str, $match); echo $match[0]; //结果为:010-12345678懒惰模式:在可匹配与可不匹配的时候,优先不匹配
$p = '/\d?\-\d?/'; $str = "我的电话是010-12345678"; preg_match($p, $str, $match); echo $match[0]; //结果为:0-1当我们确切的知道所匹配的字符长度的时候,可以使用{}指定匹配字符数
$p = '/\d{3}\-\d{8}/'; $str = "我的电话是010-12345678"; preg_match($p, $str, $match); echo $match[0]; //结果为:010-12345678使用贪婪模式匹配字符串中的姓名。(提示:\w匹配字母或数字或下划线,\s匹配任意的空白符,包括空格、制表符、换行符)
$p = '/name:([\w\s]+)/'; $str = "name:steven jobs"; preg_match($p, $str, $match); echo $match[1]; //结果为:steven jobs
使用正则表达式进行匹配
使用正则表达式的目的是为了实现比字符串处理函数更加灵活的处理方式,因此跟字符串处理函数一样,其主要用来判断子字符串是否存在、字符串替换、分割字符串、获取模式子串等。PHP使用PCRE库函数来进行正则处理,通过设定好模式,然后调用相关的处理函数来取得匹配结果。
preg_match用来执行一个匹配,可以简单的用来判断模式是否匹配成功,或者取得一个匹配结果,他的返回值是匹配成功的次数0或者1,在匹配到1次以后就会停止搜索。
$subject = "abcdef"; $pattern = '/def/'; preg_match($pattern, $subject, $matches); print_r($matches); //结果为:Array ( [0] => def )上面的代码简单的执行了一个匹配,简单的判断def是否能匹配成功,但是正则表达式的强大的地方是进行模式匹配,因此更多的时候,会使用模式:
$subject = "abcdef"; $pattern = '/a(.*?)d/'; preg_match($pattern, $subject, $matches); print_r($matches); //结果为:Array ( [0] => abcd [1] => bc )通过正则表达式可以匹配一个模式,得到更多的有用的数据。
例:编写代码使用preg_match匹配字符串中的邮箱,并输出该邮箱。
$subject = "my email is spark@imooc.com"; $pattern = '/[\w\-]+@\w+\.\w+/'; preg_match($pattern, $subject, $matches); echo $matches[0];
查找所有匹配结果
preg_match只能匹配一次结果,但很多时候我们需要匹配所有的结果,preg_match_all可以循环获取一个列表的匹配结果数组。$p = "|]+>(.*?)[^>]+>|i"; $str = "<b>example: </b><div align="left">this is a test</div>"; preg_match_all($p, $str, $matches); print_r($matches);可以使用preg_match_all匹配一个表格中的数据:
$p = "/<tr> <td>(.*?)\s*</td> <td>(.*?)\s*/i"; $str = "<table> <tr> <td>Alex</td> <td>25</td> </tr> <tr> <td>John</td> <td>26</td> </tr> </table>"; preg_match_all($p, $str, $matches); print_r($matches);<span>$matches结果排序为$matches[0]保存完整模式的所有匹配, $matches[1] 保存第一个子组的所有匹配,以此类推。</span><br><span>例:使用preg_match_all匹配所有li标签中的数据。</span><br><pre class="brush:php;toolbar:false"><?php $str = "<ul>
正则表达式的搜索和替换
正则表达式的搜索与替换在某些方面具有重要用途,比如调整目标字符串的格式,改变目标字符串中匹配字符串的顺序等。例如我们可以简单的调整字符串的日期格式:
$string = 'April 15, 2014'; $pattern = '/(\w+) (\d+), (\d+)/i'; $replacement = '$3, ${1} $2'; echo preg_replace($pattern, $replacement, $string); //结果为:2014, April 15其中${1}与$1的写法是等效的,表示第一个匹配的字串,$2代表第二个匹配的。
通过复杂的模式,我们可以更加精确的替换目标字符串的内容。
$$patterns = array ('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/', '/^\s*{(\w+)}\s*=/'); $replace = array ('\3/\4/\1\2', '$\1 =');//\3等效于$3,\4等效于$4,依次类推 echo preg_replace($patterns, $replace, '{startDate} = 1999-5-27'); //结果为:$startDate = 5/27/1999 //详细解释下结果:(19|20)表示取19或者20中任意一个数字,(\d{2})表示两个数字,(\d{1,2})表示1个或2个数字,(\d{1,2})表示1个或2个数字。^\s*{(\w+)\s*=}表示以任意空格开头的,并且包含在{}中的字符,并且以任意空格结尾的,最后有个=号的。用正则替换来去掉多余的空格与字符:
$$str = 'one two'; $str = preg_replace('/\s+/', ' ', $str); echo $str; // 结果改变为'one two'例:将目标字符串$str中的文件名替换后增加em标签,例如index.php要替换成index.php。
$str = '主要有以下几个文件:index.php, style.css, common.js'; //将目标字符串$str中的文件名替换后增加em标 $p = '/\w+\.\w+/i'; $str = preg_replace($p, '<em>$0</em>', $str); echo $str;
正则匹配常用案例
正则匹配常用在表单验证上,一些字段会有一定的格式要求,比如用户名一般都要求必须是字母、数字或下划线组成,邮箱、电话等也都有自己的规则,因此使用正则表达式可以很好的对这些字段进行验证。我们通过案例来看一下一般的用户注册页,都怎样对字段进行验证。
<?php $user = array( 'name' => 'marchalex', 'email' => 'marchalex@163.com', 'mobile' => '13312345678' ); //进行一般性验证 if (empty($user)) { die('用户信息不能为空'); } if (strlen($user['name']) <p> 以上就介绍了PHP学习笔记(五)正则表达式,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。</p> <p> </p>

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.