Replacing patterns in a string

半城伤御伤魂 提交于 2021-01-29 02:10:19

问题


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 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

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 = "")

which gives:

"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?


回答1:


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"


来源:https://stackoverflow.com/questions/28999698/replacing-patterns-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!