22

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

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

Any ideas?

djechlin
  • 59,258
  • 35
  • 162
  • 290
Jesse
  • 388
  • 6
  • 16

1 Answers1

25

You just need to switch to PERL regular expressions by setting perl = TRUE.

joran
  • 169,992
  • 32
  • 429
  • 468
  • 1
    Regardless of the simplicity of the issue. I was still missing that small part. Thanks. – Jesse Nov 16 '12 at 16:35
  • @joran, can you please explain what exactly `Perl` is doing? I searched and found this `perl: logical. Should perl-compatible regexps be used? Has priority over extended` here , http://www.endmemo.com/r/sub.php. but it's still not clear for me – Ross_you Nov 07 '20 at 03:09