php中preg正则函数使用
match
str
1.preg_match和preg_match_all的区别
preg_match和 preg_match_all区别是preg_match只匹配一次。而preg_match_all全部匹配,直到字符串结束。例:
2.贪婪模式和非贪婪模式的区别
如:String str="abcaxc";
Patter p="ab*c";
贪婪匹配:正则表达式一般趋向于最大长度匹配,也就是所谓的贪婪匹配。如上面使用模式p匹配字符串str,结果就是匹配到:abcaxc(ab*c)。
非贪婪匹配:就是匹配到结果就好,较少的匹配字符。如上面使用模式p匹配字符串str,结果就是匹配到:abc(ab*c)。
例:
3.preg_match_all参数PREG_PATTERN_ORDER(默认)和PREG_SET_ORDER的区别
延伸阅读:preg_match_all使用实例
preg_match和 preg_match_all区别是preg_match只匹配一次。而preg_match_all全部匹配,直到字符串结束。例:
<?php //注:正则 /a.+?e/ 是非贪婪模式(因为量词‘+’后面加上了‘?’),如果使用 /a.+?e/U 则变回了贪婪模式 preg_match("/a.+?e/","abcdefgabcdefgabcdefg",$out1); preg_match_all("/a.+?e/","abcdefgabcdefgabcdefg",$out2); var_dump($out1); var_dump($out2); /* 输出: array (size=1) 0 => string 'abcde' (length=5) array (size=1) 0 => array (size=3) 0 => string 'abcde' (length=5) 1 => string 'abcde' (length=5) 2 => string 'abcde' (length=5) */ ?>
Copy after login
如:String str="abcaxc";
Patter p="ab*c";
贪婪匹配:正则表达式一般趋向于最大长度匹配,也就是所谓的贪婪匹配。如上面使用模式p匹配字符串str,结果就是匹配到:abcaxc(ab*c)。
非贪婪匹配:就是匹配到结果就好,较少的匹配字符。如上面使用模式p匹配字符串str,结果就是匹配到:abc(ab*c)。
例:
<?php $str = "http://www.baidu/.com?url=www.sina.com/"; preg_match("/http:(.*)com/", $str, $matches1); //贪婪匹配 var_dump($matches1); preg_match("/http:(.*?)com/", $str, $matches2); //非贪婪匹配(量词'*'后面跟上了'?') var_dump($matches2); /* array (size=2) 0 => string 'http://www.baidu/.com?url=www.sina.com' (length=38) 1 => string '//www.baidu/.com?url=www.sina.' (length=30) array (size=2) 0 => string 'http://www.baidu/.com' (length=21) 1 => string '//www.baidu/.' (length=13) */ ?>
Copy after login
<?php echo('PREG_PATTERN_ORDER'); preg_match_all("<[^>]+>(.*)</[^>]+>U", "<b>start: </b><b>this is a test</b><b>end</b>", $out1); var_dump($out1); echo('PREG_SET_ORDER'); preg_match_all("<[^>]+>(.*)</[^>]+>U", "<b>start: </b><b>this is a test</b><b>end</b>", $out2, PREG_SET_ORDER); var_dump($out2); /* PREG_PATTERN_ORDER array (size=2) 0 => array (size=3) 0 => string '<b>start: </b>' (length=14) 1 => string '<b>this is a test</b>' (length=21) 2 => string '<b>end</b>' (length=10) 1 => array (size=3) 0 => string 'start: ' (length=7) 1 => string 'this is a test' (length=14) 2 => string 'end' (length=3) PREG_SET_ORDER array (size=3) 0 => array (size=2) 0 => string '<b>start: </b>' (length=14) 1 => string 'start: ' (length=7) 1 => array (size=2) 0 => string '<b>this is a test</b>' (length=21) 1 => string 'this is a test' (length=14) 2 => array (size=2) 0 => string '<b>end</b>' (length=10) 1 => string 'end' (length=3) */ ?>
Copy after login
延伸阅读:preg_match_all使用实例
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

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
Assassin's Creed Shadows: Seashell Riddle Solution
4 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
3 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
4 weeks ago
By DDD
Roblox: Dead Rails - How To Complete Every Challenge
1 months ago
By DDD
How to fix KB5055523 fails to install in Windows 11?
2 weeks ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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