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

R中的正则表达式:查找后跟模式的数字,r,regex,R,Regex,我正在尝试制作一个正则表达式,它可以在模式匹配后提取来自任何位置的数字 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",

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

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

df您可以使用
sub
提取
“price”
后面的数字

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

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

您可以使用
sub
提取
“price”
后面的数字

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

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

我已经编辑了我的问题。我正在寻找第一个数字后,价格和数字可以后面跟着/或-或更好!“价格是12+25”
怎么样?不清楚您是要返回“价格12”还是不匹配。换句话说,比赛之后会发生什么?再次,请通过编辑澄清。我不知道R,但有些东西看起来很奇怪。
str\u extract
的参数应该是
df
而不是
df$V1
?我相信在你编辑之前,
df
已经是
df$V1
。我已经编辑了我的问题。我正在寻找第一个数字后,价格和数字可以后面跟着/或-或更好!“价格是12+25”怎么样?不清楚您是要返回“价格12”还是不匹配。换句话说,比赛之后会发生什么?再次,请通过编辑澄清。我不知道R,但有些东西看起来很奇怪。
str\u extract
的参数应该是
df
而不是
df$V1
?我相信在您编辑之前,
df
已经是
df$V1
。非常感谢Ronak。我只查找后跟价格的数字的第一个实例。@RBan我已更新了新数据集的答案。你能看一下吗?非常感谢Ronak。我只查找后跟价格的数字的第一个实例。@RBan我已更新了新数据集的答案。你能看一下吗?