简体   繁体   English

gsub:如果没有用括号括起来,则替换单词

[英]gsub: replace word if not wrapped in brackets

I would like to gsub a word, but only cases where it is not wrapped in brackets.我想gsub一个单词,但仅限于它没有用括号括起来的情况。

x <- c("hello","[hello]")

I would like gsub(regex,"test",x) to return c("test","[hello]") , but I am having trouble creating the correct regex statement.我希望gsub(regex,"test",x)返回c("test","[hello]") ,但我无法创建正确的正则表达式语句。

A naive implementation is: gsub("^(?!\\\\[).*$","test",x, perl=TRUE) , which works in the above case, but only because each string is one word, so it doesn't work for x <- "hello [hello]" for example, which I want to be test [hello] .一个简单的实现是: gsub("^(?!\\\\[).*$","test",x, perl=TRUE) ,它适用于上述情况,但只是因为每个字符串都是一个单词,所以它不适用于x <- "hello [hello]"例如,我想test [hello]

I've tried a bunch of different lookaheads to no avail.我已经尝试了一堆不同的前瞻无济于事。 Any help would be appreciated.任何帮助,将不胜感激。


Input输入

x <- c("hello", "[hello]", "hello [hello]")

Desired想要的

# [1] "test"         "[hello]"      "test [hello]"

You can use negative look around to set constraint to the word boundaries, for instance (?<!\\\\[)\\\\b\\\\w+\\\\b(?!\\\\]) will replace words only if the word boundary is not [] :您可以使用否定环顾来设置对单词边界的约束,例如(?<!\\\\[)\\\\b\\\\w+\\\\b(?!\\\\])仅当单词边界不是[] :

gsub("(?<!\\[)\\b\\w+\\b(?!\\])", "test", x, perl = TRUE)
# [1] "test [hello]"       # assuming this is your desired output

\\\\b\\\\w+\\\\b will look for a word but with negative look-behind ?<! \\\\b\\\\w+\\\\b将查找一个单词,但带有否定的后视?<! and negative look-ahead ?!和负面展望?! , the word boundary should not be [] . ,单词边界不应该是[] You can also reference this answer .你也可以参考这个答案

We can do this easily with grep我们可以使用grep轻松做到这一点

x[grep("^[^[]+$", x)] <- "test"
x
#[1] "test"    "[hello]"

Or with sub或与sub

sub("^[^[]+", "test", x)
#[1] "test"    "[hello]"

For the second case对于第二种情况

sub("^\\b[^[+]+\\b", "test", x1)
#[1] "test [hello]"

data数据

x <- c("hello","[hello]")
x1 <- "hello [hello]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM