我很确定我在这里缺少一些明显的东西,但是我不能使R使用非贪心的正则表达式:
@H_301_22@> library(stringr) > str_match('xxx aaaab yyy',"a.*?b") [,1] [1,] "aaaab"
基本功能的行为方式相同:
> regexpr('a.*?b','xxx aaaab yyy') [1] 5 attr(,"match.length") [1] 5 attr(,"useBytes") [1] TRUE
我希望比赛根据http://stat.ethz.ch/R-manual/R-devel/library/base/html/regex.html中的“贪婪”评论只是ab:
By default repetition is greedy,so the maximal possible number of repeats is used.
This can be changed to ‘minimal’ by appending ? to the quantifier. (There are further
quantifiers that allow approximate matching: see the TRE documentation.)
有人可以解释一下我发生了什么吗?
更新.疯狂的是,在其他一些情况下,非贪婪模式的行为与预期的一样:
> str_match('xxx <a href="abc">link</a> yyy <h1>Header</h1>','<a.*>') [,1] [1,] "<a href=\"abc\">link</a> yyy <h1>Header</h1>" > str_match('xxx <a href="abc">link</a> yyy <h1>Header</h1>','<a.*?>') [,1] [1,] "<a href=\"abc\">"