简体   繁体   English

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

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

I am trying to make a regular expression which can extract number coming at any place after a pattern is matched.我正在尝试制作一个正则表达式,它可以在模式匹配后提取来自任何地方的数字。

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+.*")

My expected output in v2, basically first number after price and can be /- or space followed by number(2/1 or 2-1 or 2 1: price 2/1我在 v2 中预期的 output,基本上是价格后的第一个数字,可以是 /- 或空格后跟数字(2/1 或 2-1 或 2 1:价格 2/1
price 20-1价格 20-1
price 120价格 120
price 30 1价格 30 1
price 10价格 10
Not Found未找到
Regards, R问候, R

You can use sub to extract digits which come after "price" .您可以使用sub提取"price"之后的数字。

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

For updated data we can use:对于更新的数据,我们可以使用:

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