extract number after specific string

我与影子孤独终老i 提交于 2019-11-26 14:47:38

问题


I need to find the number after the string "Count of". There could be a space or a symbol between the "Count of" string and the number. I have something that works on www.regex101.com but does not work with stringr str_extract function.

library(stringr)

shopping_list <- c("apples x4", "bag of flour", "bag of sugar", "milk x2", "monkey coconut 3oz count of 5", "monkey coconut count of 50", "chicken Count Of-10")
str_extract(shopping_list, "count of ([\\d]+)")
[1] NA NA NA NA "count of 5" "count of 50" NA

What I want to get:

[1] NA NA NA NA "5" "50" "10"

回答1:


str_extract(shopping_list, "(?i)(?<=count of\\D)\\d+")
# [1] NA   NA   NA   NA   "5"  "50" "10"

where (?i) makes the pattern case insensitive, \\D means not a number, and ?<= is a positive lookbehind.




回答2:


Look ahead and look behinds are what you are looking for with this grep...

shopping_list <- c("apples x4", "bag of flour", "bag of sugar", "milk x2", "monkey coconut 3oz count of 5", "monkey coconut count of 50", "chicken Count Of-10")
str_extract(shopping_list, "(?<=count of )[0-9]*")
[1] NA   NA   NA   NA   "5"  "50" NA  



回答3:


as.numeric(sub("(?i).*count of.*?(\\d+).*", "\\1", shopping_list))
[1] NA NA NA NA  5 50 10

The regex pattern is:

  • (?i): Ignore case
  • .*count of.*?: Any length of characters up to "count of"
  • (\\d+): Capture one or more digits
  • "\\1": Return the capture group

As of now the other answers will fail with something like ""coconut count of - 5" since they are constrained by one space after "count of".



来源:https://stackoverflow.com/questions/35947123/extract-number-after-specific-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!