首页 > 其他 > 详细

Regex plus vs star difference?

时间:2020-12-14 18:50:19      阅读:24      评论:0      收藏:0      [点我收藏+]

Regex plus vs star difference?

What is the difference between:

(.+?)

and

(.*?)

when I use it in my php preg_match regex?

 

They are called quantifiers.

* 0 or more of the preceding expression

+ 1 or more of the preceding expression

Per default a quantifier is greedy, that means it matches as many characters as possible.

The ? after a quantifier changes the behaviour to make this quantifier "ungreedy", means it will match as little as possible.

Example greedy/ungreedy

For example on the string "abab"

a.*b will match "abab" (preg_match_all will return one match, the "abab")

while a.*?b will match only the starting "ab" (preg_match_all will return two matches, "ab")

You can test your regexes online e.g. on Regexr, see the greedy example here

技术分享图片

 

 

技术分享图片

 

 

 

 

 

 

Regex plus vs star difference?

原文:https://www.cnblogs.com/chucklu/p/14133030.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!