在 R 中使用正则表达式的某个字符串模式之后的所有第一个数字 [英] All first numbers after some string pattern using regex in R

查看:28
本文介绍了在 R 中使用正则表达式的某个字符串模式之后的所有第一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取字符串mystr"和其他内容之后的所有数字.例如,如果我有字符串.

I want to extract all numbers after string "mystr" and something else. For example if I have string.

x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."

它应该返回 8 和 12.在 R 我试过:

It should return 8 and 12. In R I tried:

stringr::str_extract_all(x, "mystr.*\\d+")

推荐答案

mystr 使用

x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
regmatches(x, gregexpr("mystr.*?\\K\\d+", x, perl=TRUE))
# => [[1]]
#    [1] "8"  "12"

查看 R 演示

这个 PCRE 正则表达式将匹配

This PCRE regex will match

  • mystr - mystr
  • .*? - 尽可能少的除换行符以外的任何 0+ 个字符
  • \\K - 将忽略目前匹配的文本
  • \\d+ - 1+ 位数字.
  • mystr - mystr
  • .*? - any 0+ chars other than line break chars as few as possible
  • \\K - will omit the text matched so far
  • \\d+ - 1+ digits.

请参阅 PCRE 正则表达式演示.

如果你想使用stringr,你可以使用str_match_all:

If you want to use stringr, you may use str_match_all:

> library(stringr)
> x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
> str_match_all(x, "mystr.*?(\\d+)")[[1]][,2]
[1] "8"  "12"

是否将数字捕获到组 1 中.

were the digits are captured into Group 1.

这篇关于在 R 中使用正则表达式的某个字符串模式之后的所有第一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆