Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting decimal numbers from a string

Tags:

string

regex

r

I have a string such as "3.1 ml" or "abc 3.1 xywazw"

I'd like to extract "3.1" from this string. I have found many questions on stackoverflow about the extraction of numbers from a character string, but no solution works for the case of decimal numbers.

like image 408
Stéphane Laurent Avatar asked Oct 08 '13 15:10

Stéphane Laurent


People also ask

How do I extract a decimal from a string in Excel?

Select a cell and type this formula =A1-TRUNC(A1) (A1 is the cell you want to extract decimal value from) into the Formula Bar, and then press Enter key. Keep selecting the first result cell, and drag fill handle down to get all results. You can see the decimal values are extracted with sign as below screenshot shown.

How do I extract a decimal from a string in Python?

We use \d+\. \d+ regular expression in python to get non digit characters from a string. Where, \d returns a match where the string contains digits (numbers from 0 9)

How do you extract a decimal number?

The TRUNC function simply truncates (i.e. removes) decimal values if they exist – it doesn't do any rounding. The TRUNC function returns the integer portion of the number which is then subtracted from the original value. The result is the decimal portion of the number.


4 Answers

This approach makes the decimal point and decimal fraction optional and allows multiple numbers to be extracted:

str <- " test 3.1 test 5"
as.numeric(unlist(regmatches(str,
                             gregexpr("[[:digit:]]+\\.*[[:digit:]]*",str))
          )      )
#[1] 3.1 5.0

The concern about negative numbers can be address with optional perl style look-ahead:

 str <- " test -4.5 3.1 test 5"
    as.numeric(unlist(regmatches(str,gregexpr("(?>-)*[[:digit:]]+\\.*[[:digit:]]*",str, perl=TRUE))))

#[1] -4.5  3.1  5.0
like image 64
IRTFM Avatar answered Oct 08 '22 02:10

IRTFM


Use the stringr library:

x<-"abc 3.1 xywazw"
str_extract(x, "\\d+\\.*\\d*")
[1] "3.1"
like image 18
tcash21 Avatar answered Oct 08 '22 02:10

tcash21


Regular expression for floating point number from http://www.regular-expressions.info/floatingpoint.html with minor adjustment to work in R.

s <- "1e-6 dkel"
regmatches(s,gregexpr("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?",s)) 
> [[1]]
> [1] "1e-6"
like image 8
Wojciech Sobala Avatar answered Oct 08 '22 03:10

Wojciech Sobala


You can use regular expressions :

> str <- " test 3.1 test"
> as.numeric(regmatches(str,regexpr("[[:digit:]]+\\.[[:digit:]]+",str)))
[1] 3.1

regexprreturns the start position and length of the matched string. regmatchesreturns the matches. You can then convert it to a number.

like image 1
Thibaud Ruelle Avatar answered Oct 08 '22 03:10

Thibaud Ruelle