-1

I wish to extract the decimal value in the string without the % sign. So in this case, I want the numeric 0.45

x <- "document.write(GIC_annual[\"12-17 MTH\"][\"99999.99\"]);0.450%"
str_extract(x, "^;[0-9.]")

My attempt fails. Here's my thinking.

  • Begin the extraction at the semicolon ^;
  • Grab any numbers between 0 and 9.
  • Include the decimal point
ixodid
  • 2,180
  • 1
  • 19
  • 46

2 Answers2

1

You also have this option:

stringr::str_extract(y, "\\d\\.\\d{1,}(?=%)")
[1] "0.450"

So basically you look ahead and check if there is % or not, if yes, you capture the digits before it.

Details

\\d digit;

\\. dot;

\\d digit;

{1,} capturing 1 or more digit after .;

(?=%) look ahead and check if there is % and if there is one, it retuns captured number

Ross_you
  • 881
  • 5
  • 22
  • So it searches for a % sign - (?=%). If it finds one, then it looks before the % sign for a digit followed by a decimal and then any number of digits. Is that it? – ixodid Nov 13 '20 at 03:58
  • Exactly, if (?=%) is true, then it goes back and finds the pattern you want – Ross_you Nov 13 '20 at 04:06
0

Since you don't want semi-colon in the output use it as lookbehind regex.

stringr::str_extract(x, "(?<=;)[0-9]\\.[0-9]+")
#[1] "0.450"

In base R using sub :

sub('.*;([0-9]\\.[0-9]+).*', '\\1', x)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213