4

任何人都可以通过一些合适的例子帮助我理解两种正则表达式方法之间的区别吗?

  • 贪婪的
  • 不贪心

谢谢

4

3 回答 3

9

在贪婪的方法中,正则表达式模式倾向于消耗源字符串中的最大字符。例如

textstr = "bcabdcab"
textstr.gsub!(/(.*)ab/, "xxx")  
# this will match whole `bcabdcab` and return `xxx`

*是一个贪婪的量词。在非贪婪方法中,正则表达式引擎在满足匹配条件时返回。使量词非贪婪追加?

textstr = "bcabdcab"
textstr.gsub!(/(.*?)ab/, "xxx")  
# this will match only `bcab` part and return `xxxdcab`

gsub返回 str(first argument) 的副本,其中所有出现的模式都替换为第二个参数

于 2013-01-15T14:51:06.473 回答
3

查看http://www.regular-expressions.info/repeat.html

贪婪是指正则表达式引擎尝试匹配特定字符集的次数。陈述正则表达式“贪婪”的方法是使用特殊字符*,+和.?{}

考虑

str = "asdfasdfbbbb"
r1 = /b/
r2 = /(asdf)*/
r3 = /b{3}/
r4 = /.*/

将这些正则表达式与str匹配将导致:

r1 matching "asdfasdf b bbb" (non-greedy, tries to match b just once)
r2 matching "asdfasdf bbbb" (greedy, tries to match asdf as many times as possible)
r3 matching "asdfasdf bbb b" (non-greedy, matches b exactly 3 times)
r4 matching "asdfasdfbbbb" (ULTRA-greedy, matches almost any character as many times as possible)

As regex are means to represent specific text patterns, it's not like greediness it's a matter of approach. You will sometimes need to match three times foo(/(foo){3}/) or infinite times bar(/(bar)*/).

于 2013-01-15T15:07:41.073 回答
2
  • *- (0个或更多)贪婪匹配
  • +- (1个或更多)贪婪匹配
  • *?- (0个或更多)非贪婪匹配
  • +?- (1个或多个)非贪婪匹配
于 2013-01-15T14:41:32.240 回答