小编San*_*osh的帖子

在 R 中查找特定模式后任意位置的第一个数字

我正在尝试识别 R 中某个子字符串之后出现的数字。

例如:

sa <- "100 dollars 200"
Run Code Online (Sandbox Code Playgroud)

在上面的字符串中,为了查找单词 后面出现的数字dollar,我执行以下代码:

str_match_all(sa,"(?<=dollars )\\d+") 
Run Code Online (Sandbox Code Playgroud)

我得到以下结果:

  [[1]]
     [,1] 
[1,] "200"
Run Code Online (Sandbox Code Playgroud)

但是,当我使用以下输入时:

sa <- "100 dollars for 200 pesos"
Run Code Online (Sandbox Code Playgroud)

我非常无法获得输出200

regex r regex-lookarounds

2
推荐指数
1
解决办法
2448
查看次数

标签 统计

r ×1

regex ×1

regex-lookarounds ×1