替换字符串中的模式 [英] Replacing patterns in a string

查看:40
本文介绍了替换字符串中的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几种这种格式的字符串.分隔符是一个破折号(-),中间的每个事物"都是一个标记.

I have several strings in this format. The separator is a dash (-) and each "thing" in between is a marker.

string <- "FA-I2-I2-I2-EX-I2-I3-FA-I1-I2-TR-I1-I2-FA-I3-I1-FAFANR-I3-I2-TR-I1-I2-I1-I2-FA-I2-I1-I3-FAQU-I1-I2-I2-I2-NR-I2-I2-NR-I1-I2-I1-NR-I3-QU-I2-I3-QUNR-I2-I1-NRQUQU-I2-I1-EX"

我想确定连续出现包含字母"I"的标记(即标记I1,I2和I3)的情况.然后,我想用没有分隔符的描述替换那些描述.例如,字符串的开头应按如下所示进行转换:

I want to identify cases wherever markers containing the letter "I" occurs in a row (i.e. the markers I1, I2, and I3). Then I want to replace those with a description that has no separators. For example, the very beginning of the string should be converted as follows:

FA-I2I2I2-EX

因此,基本上我想要做的就是删除包含"I"的标记之间的所有破折号.

So basically all I want to do is to remove all the dashes between markers containing "I".

这是一个令人费解的解决方案:

Here's a somewhat convoluted solution:

string1 <- gsub(string, pattern = "I1", replacement = "ZI1Z")
string2 <- gsub(string1, pattern = "I2", replacement = "ZI2Z")
string3 <- gsub(string2, pattern = "I3", replacement = "ZI3Z")
string4 <- gsub(string3, pattern = "Z-Z", replacement = "")
string5 <- gsub(string4, pattern = "Z", replacement = "")

给出:

"FA-I2I2I2-EX-I2I3-FA-I1I2-TR-I1I2-FA-I3I1-FAFANR-I3I2-TR-I1I2I1I2-FA-I2I1I3-FAQU-I1I2I2I2-NR-I2I2-NR-I1I2I1-NR-I3-QU-I2I3-QUNR-I2I1-NRQUQU-I2I1-EX"

有没有更优雅的方式来实现这一目标?

Is there a more elegant way of accomplishing this?

推荐答案

因此,基本上我想要做的就是删除包含"I"的标记之间的所有破折号.

So basically all I want to do is to remove all the dashes between markers containing "I".

如果您的案子听起来很简单,就可以使用环视断言.

You can use lookaround assertions if your case is as simple as it sounds.

gsub('(?<=I\\d)-(?=I\\d)', '', string, perl = TRUE)
# [1] "FA-I2I2I2-EX-I2I3-FA-I1I2-TR-I1I2-FA-I3I1-FAFANR-I3I2-TR-I1I2I1I2-FA-I2I1I3-FAQU-I1I2I2I2-NR-I2I2-NR-I1I2I1-NR-I3-QU-I2I3-QUNR-I2I1-NRQUQU-I2I1-EX"

这篇关于替换字符串中的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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