regex - 提取特定字符串后的数字

标签 regex r stringr

我需要找到字符串“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"

最佳答案

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

哪里(?i)使模式不区分大小写,\\D表示不是数字,并且 ?<=是积极的回顾。

关于regex - 提取特定字符串后的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35947123/

相关文章:

regex - str_extract_all 返回不匹配的组

r - 字符串问题 : remove patterns, "//, "///,“////,位于字符串末尾

r - Stringr str_replace_all 遗漏重复项

php - 如何从 PHP 中的文本中删除空行?

r - 使用 R 创建与 twitter 流 API 的持久连接

r - ggplot2 中辅助 y 轴的更好转换

r - ggplot2 -- 自动放大 geom_smooth(使用 coord_cartesian)

java - 使用模式将多个字符串与一个长字符串匹配

regex - 检查字符串是否包含数字或者是数字 - Thymeleaf

regex - VBScript RegExp 不够贪心