特定字符串后提取数字

时间:2016-03-11 18:13:08

标签: regex r stringr

我需要在字符串“Count of”之后找到数字。 “Count of”字符串和数字之间可能有空格或符号。我有一些适用于www.regex101.com的内容,但不适用于stringr str_extract函数。

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

我想得到什么:

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

3 个答案:

答案 0 :(得分:2)

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

其中(?i)使模式不区分大小写,\\D表示不是数字,而?<=是正面的背后隐藏。

答案 1 :(得分:0)

liip_search:
clients:
    google_rest:
        api_key: '%google.api_key%'
        search_key: '%google.search_key%'

正则表达式模式是:

  • as.numeric(sub("(?i).*count of.*?(\\d+).*", "\\1", shopping_list)) [1] NA NA NA NA 5 50 10 :忽略大小写
  • (?i):任意长度的字符,直到&#34;计数为&#34;
  • .*count of.*?:捕获一个或多个数字
  • (\\d+):返回捕获组

到目前为止,其他答案将因"\\1"之类的问题而失败,因为它们在&#34;&#34;&#34;之后被一个空格约束。

答案 2 :(得分:0)

向前看,看看背后是你正在寻找的这个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