R正则表达式向后看 [英] R Regular Expression Lookbehind

查看:117
本文介绍了R正则表达式向后看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个填充有以下格式字符串的向量:<year1><year2><id1><id2>

I have a vector filled with strings of the following format: <year1><year2><id1><id2>

向量的第一项如下:

199719982001
199719982002
199719982003
199719982003

对于第一个条目,我们有:year1 = 1997,year2 = 1998,id1 = 2,id2 =001.

For the first entry we have: year1 = 1997, year2 = 1998, id1 = 2, id2 = 001.

我想写一个正则表达式,提取出year1,id1和id2的数字不为零.因此,对于第一个条目,正则表达式应输出:199721.

I want to write a regular expression that pulls out year1, id1, and the digits of id2 that are not zero. So for the first entry the regex should output: 199721.

我尝试使用stringr软件包执行此操作,并创建了以下正则表达式:

I have tried doing this with the stringr package, and created the following regex:

"^\\d{4}|\\d{1}(?<=\\d{3}$)"

拉出year1和id1,但是在使用lookbehind时出现无效的正则表达式"错误.这让我有些困惑,R不能处理前瞻和后顾之忧吗?

to pull out year1 and id1, however when using the lookbehind i get a "invalid regular expression" error. This is a bit puzzling to me, can R not handle lookaheads and lookbehinds?

推荐答案

由于这是固定格式,为什么不使用substr?使用substr(s,1,4)提取year1,使用substr(s,9,9)提取id1,将id2用作as.numeric(substr(s,10,13)).在最后一种情况下,我使用as.numeric消除了零.

Since this is fixed format, why not use substr? year1 is extracted using substr(s,1,4), id1 is extracted using substr(s,9,9) and the id2 as as.numeric(substr(s,10,13)). In the last case I used as.numeric to get rid of the zeroes.

这篇关于R正则表达式向后看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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