为什么这个正则表达式在 R 中使用后视无效? [英] Why is this regex using lookbehinds invalid in R?
本文介绍了为什么这个正则表达式在 R 中使用后视无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 R 中进行后视正则表达式以找到模式.我希望这会拉动 'bob' 中的 'b',但我得到了一个错误.
I'm trying to do a lookbehind regex in R to find a pattern. I expect this would pull the 'b' in 'bob', but instead I get an error.
> regexpr("(?<=a)b","thingamabob")
Error in regexpr("(?<=a)b", "thingamabob") :
invalid regular expression '(?<=a)b', reason 'Invalid regexp'
这不会抛出错误,但也没有找到任何东西.
This does not throw an error, but it also doesn't find anything.
> regexpr("(.<=a)b","thingamabob")
[1] -1
attr(,"match.length")
[1] -1
attr(,"useBytes")
[1] TRUE
我很困惑,因为 regexpr 的帮助页面明确指出后视应该可以工作:http://stat.ethz.ch/R-manual/R-patched/library/base/html/regex.html
I'm confused because the help page for regexpr specifically indicates that lookbehind should work: http://stat.ethz.ch/R-manual/R-patched/library/base/html/regex.html
有什么想法吗?
推荐答案
你只需要通过设置 perl = TRUE
来切换到 PERL 正则表达式.
You just need to switch to PERL regular expressions by setting perl = TRUE
.
这篇关于为什么这个正则表达式在 R 中使用后视无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文