简体   繁体   中英

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
price 20-1
price 120
price 30 1
price 10
Not Found
Regards, R

You can use sub to extract digits which come after "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   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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