简体   繁体   English

从字符串中提取十进制数

[英]Extracting decimal numbers from a string

I have a string such as "3.1 ml" or "abc 3.1 xywazw" 我有一个字符串,如"3.1 ml""abc 3.1 xywazw"

I'd like to extract "3.1" from this string. 我想从这个字符串中提取"3.1" 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. 我在stackoverflow上发现了很多关于从字符串中提取数字的问题,但没有解决方案适用于十进制数字的情况。

Use the stringr library: 使用stringr库:

x<-"abc 3.1 xywazw"
str_extract(x, "\\d+\\.*\\d*")
[1] "3.1"

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: 关于负数的担忧可以通过可选的perl样式预测来解决:

 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

Regular expression for floating point number from http://www.regular-expressions.info/floatingpoint.html with minor adjustment to work in R. 来自http://www.regular-expressions.info/floatingpoint.html的浮点数的正则表达式,并在R中进行微调。

s <- "1e-6 dkel"
regmatches(s,gregexpr("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?",s)) 
> [[1]]
> [1] "1e-6"

You can use regular expressions : 您可以使用正则表达式:

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

regexpr returns the start position and length of the matched string. regexpr返回匹配字符串的起始位置和长度。 regmatches returns the matches. regmatches返回匹配。 You can then convert it to a number. 然后,您可以将其转换为数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM