繁体   English   中英

R 中的正则表达式:查找数字后跟模式

[英]Regex in R : Find number followed by a pattern

我正在尝试制作一个正则表达式,它可以在模式匹配后提取来自任何地方的数字。

df <-as.data.frame(cbind(c("The 100 price of apple is 2/1 and could be more than 30 ",
                           "The 200 price of fruits can be 20-1  and I am not sure how much it can decrease it can be 1", 
                           "The price is 120", 
                           "The price can be anything but less than 30 1", 
                           "The price 10",'there is price')))
df$v2 <- str_extract(df$V1, "price[^a-zA-Z]+\\d+.*")

我在 v2 中预期的 output,基本上是价格后的第一个数字,可以是 /- 或空格后跟数字(2/1 或 2-1 或 2 1:价格 2/1
价格 20-1
价格 120
价格 30 1
价格 10
未找到
问候, R

您可以使用sub提取"price"之后的数字。

sub('.*price.*?(\\d+)', '\\1', df$V1)
#[1] "2/1"  "20-1" "120"  "30 1" "10"  

对于更新的数据,我们可以使用:

stringr::str_match(df$V1, '.*price.*?(\\d+[-/ ]?\\d+?).*')[, 2]
#[1] "2/1"  "20-1" "120"  "30 1" "10"   NA   

暂无
暂无

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

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