2

I'm running R 3.2.1 and stringr 1.0.0 and the following works as expected on my machine. The goal is to extract the number associated with pounds (130), which the following accomplishes on my machine.

x <- "line 4322: He is now 25 years old, and weights 130lbs"

x %>% 
  str_extract("\\d+(?=lbs)") %>% 
  as.numeric()
[1] 130

Again, this works fine on my machine. However, on a friends PC, it does not and the perl() function has been deprecated.

So, the goal is to continue with the stringr package but add the proper options so that it is PCRE compatible. These two posts, Post 1 and Post 2, address the issue, but they use base functions and I want to preserve the workflow I have with stringr.

This code is a possible solution, but I'm not sure if will work as expected on another machine.

x %>% 
  str_extract(regex("\\d+(?=lbs)", perl = T)) %>% 
  as.numeric()
[1] 130
Community
  • 1
  • 1
Ryan Erwin
  • 807
  • 1
  • 11
  • 30
  • 2
    In the recent versions, you don't need to specify the `perl`, previously, I had to wrap `perl` around the `regex` and now it will give the error that perl function is deprecated. So, it is better to install the new version of stringr as well as the new R version for your friend. BTW, I am using ` stringr_1.0.0` and `R 3.2.2` and both the codes work for me. – akrun Sep 07 '15 at 16:05
  • 1
    If you want to replicate the error `x %>% str_extract(perl("\\d+(?=lbs)"))# perl is deprecated. Please use regexp instead` – akrun Sep 07 '15 at 16:10
  • Thanks @akrun , I was hoping to not have to rely on updating another system. However, that might be my only option as I haven't seen a definitive answer to my specific question. – Ryan Erwin Sep 07 '15 at 17:18
  • The point here is that your pattern will work the same in both PCRE and ICU regex flavors. Most of them will work the same. There might be problems with Unicode strings and PCRE verbs won't work in ICU stringr regexps. You should just have an overview of the patterns to see if they are compatible in both flavors, and just disregard the options. Note that ICU is there to stay in *stringr*, so it is better to revamp the code/expressions to meet the ICU standard. Or re-write the code in Base R to use PCRE there with `perl=TRUE`. – Wiktor Stribiżew May 11 '17 at 08:23

0 Answers0