正则表达式 (.*?) 和 (.*) 之间有什么区别? [英] What is the difference between the regex (.*?) and (.*)?
问题描述
我已经做了一段时间的正则表达式,但我不是特定规则的微妙之处的专家,我总是用 (.*?)
进行匹配,但是用限制,正如我所理解的那样,它会停止它获得的第一次机会,而 (.*)?
会继续并且更加贪婪.
I've been doing regex for a while but I'm not an expert on the subtleties of what particular rules do, I've always done (.*?)
for matching, but with restriction, as in I understood it would stop the first chance it got, whereas (.*)?
would continue and be more greedy.
但我没有真正的理由这么想,我只是因为我从前读过它而这么想.
but I have no real reason why I think that, I just think it because I read it once upon a time.
现在我想知道,有区别吗?如果是,那是什么...
now I'd like to know, is there a difference? and if so, what is it...
推荐答案
(.*?)
是一个包含非贪婪匹配的组.
(.*?)
is a group containing a non-greedy match.
(.*)?
是包含贪婪匹配的可选组.
(.*)?
is an optional group containing a greedy match.
这篇关于正则表达式 (.*?) 和 (.*) 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!