开发者

Regular Expression to replace substring occurrences when not surrounded by square brackets

Lets say I have the following string vector:

x <- c("th开发者_如何学Gois!", "is!", "not my name[!]!", "Understrand[!] Mate!", 
"Because!I[!] said so!")

I need a way of replacing the exclamation marks "!" with "!\n" but only if the exclamation mark is not surrounded by square brackets. So the output would look like this:

"this!\n"
"is!\n"
"not my name[!]!\n"
"Understrand[!] Mate!\n"
"Because!\nI[!] said so!\n"

I've been playing around and just can't figure it out.

Many thanks in advance for any help.

Tony B.


Using a perl style regex with a negative look-behind (?<!pattern) and negative look-ahead (?!pattern) assertion:

R> gsub("(?<!\\[)\\!(?!\\])", "!\n", x, perl=TRUE)
[1] "this!\n"                   "is!\n"                    
[3] "not my name[!]!\n"         "Understrand[!] Mate!\n"   
[5] "Because!\nI[!] said so!\n"

Edit: @Mareks test cases require a Boolean "or" ("|"):

R> gsub("(?<!\\[)\\!|\\!(?!\\])", "!\n", c("aa[!bb", "aa!]bb"), perl=TRUE)
[1] "aa[!\nbb" "aa!\n]bb"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜