0

As per this link, I wrote a regex that does not give the expected result when executed for a specific string in R:

string <- "0,9% BB"
regex  <- "^ ?\\d+[\\d ,\\.]*[B-DF-HJ-NP-TV-Z\\/]*%?"   
grep(regex, string, value = T, perl = T)

The result output is

 [1] "0,9% BB"

instead of the desired (and outputed by the link)

[1] "0,9%"

What am I missing to get the desired output? Preferably base R, please.

Fabio Correa
  • 1,257
  • 1
  • 11
  • 17

1 Answers1

1

This returns "0,9%" using only base R

string <- "0,9% BB"
regex  <- "^ ?\\d+[\\d ,\\.]*[B-DF-HJ-NP-TV-Z\\/]*%?"
regmatches(x = string, m = regexpr(regex,string,perl = TRUE))
John Girardot
  • 341
  • 2
  • 5