正则表达式:在字符开始时捕获数字和在字符后捕获数字

时间:2017-08-28 17:42:48

标签: r regex

我需要捕获3.93,4.63999 ......和-5.35。我尝试了各种各样的变体,但一直无法获取正确的数字。

Copay:20.30

3.93

TAB 8.6MG数量:60

4.6399999999999997

-5.35

2,000UNIT TAB数量:30

AMOUNT

数量:180

CAP 4MG

2 个答案:

答案 0 :(得分:1)

x = c("Copay: 20.30", "3.93", "TAB 8.6MG Qty:60", "4.6399999999999997", "-5.35", "2,000UNIT TAB Qty:30", "AMOUNT", "Qty:180", "CAP 4MG");
grep("^[\\-]?\\d+[\\.]?\\d+$", x);

输出(见?grep):

[1] 2 4 5

如果允许前导/尾随空格,请使用

更改regex
"^\\s*[\\-]?\\d+[\\.]?\\d+\\s*$"

答案 1 :(得分:0)

试试这个

S <- c("Copay: 20.30", "3.93", "TAB 8.6MG Qty:60", "4.6399999999999997", "-5.35", "2,000UNIT TAB Qty:30", "AMOUNT", "Qty:180", "CAP 4MG")

library(stringr)
ans <- str_extract_all(S, "-?[[:digit:]]*(\\.|,)?[[:digit:]]+", simplify=TRUE)
clean <- ans[ans!=""]

输出

 [1] "20.30"              "3.93"               "8.6"               
 [4] "4.6399999999999997" "-5.35"              "2,000"             
 [7] "180"                "4"                  "60"                
[10] "30"