stringr str_extract capture group capturing everything

旧城冷巷雨未停 提交于 2019-12-05 04:46:45

The capture group is irrelevant in this case. The function str_extract will return the whole match including characters before and after the capture group.

You have to work with lookbehind and lookahead instead. Their length is zero.

library(stringr)
str_extract(string = 'X2015.XML.Outgoing.pounds..millions.',
            pattern = '(?<=X)\\d{4}(?=\\.)')
# [1] "2015"

This regex matches four consecutive digits that are preceded by an X and followed by a ..

Alternatively, you can use gsub:

string = 'X2015.XML.Outgoing.pounds..millions.'

gsub("X(\\d{4})\\..*", "\\1", string)
# [1] "2015"

or str_replace from stringr:

library(stringr)
str_replace(string, "X(\\d{4})\\..*", "\\1")
# [1] "2015"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!