R 如何删除包含特定字符的行

R 如何删除包含特定字符的行,r,regex,string,R,Regex,String,我正在尝试删除包含特定字符的行 这是我试图做的一个例子。我试图保留那些没有B的ID 输入 我正在寻找这样的输出 IDs WW-111 WW-112 WW_1234 正确的方法是什么?试试^[^B]+$ 分解它,这意味着 ^ start of the string [^B] any character except those in the brackets + at least one of the previous characters

我正在尝试删除包含特定字符的行

这是我试图做的一个例子。我试图保留那些没有
B
的ID 输入

我正在寻找这样的输出

IDs
WW-111
WW-112
WW_1234

正确的方法是什么?

试试
^[^B]+$

分解它,这意味着

^         start of the string
 [^B]     any character except those in the brackets
     +    at least one of the previous characters
      $   end of the string

试试
^[^B]+$

分解它,这意味着

^         start of the string
 [^B]     any character except those in the brackets
     +    at least one of the previous characters
      $   end of the string

如果您想保持简单,可以使用
stringr::str\u detect

x <- c("A", "AB", "BB", "CC", "D", "DBD")
x[!stringr::str_detect(x, "B")]

如果您想保持简单,可以使用
stringr::str\u detect

x <- c("A", "AB", "BB", "CC", "D", "DBD")
x[!stringr::str_detect(x, "B")]
在base中,您可以使用
grepl
并取消
B
的命中,如:

x[!grepl("B", x)]
[1] "WW-111"  "WW-112"  "WW_1234"
数据:

x在base中,您可以使用
grepl
并否定
B
的命中,如:

x[!grepl("B", x)]
[1] "WW-111"  "WW-112"  "WW_1234"
数据:


x我们还可以在
grep

grep("B$", x, invert = TRUE, value = TRUE)
#[1] "WW-111"  "WW-112"  "WW_1234"
数据
x我们还可以在
grep

grep("B$", x, invert = TRUE, value = TRUE)
#[1] "WW-111"  "WW-112"  "WW_1234"
数据
x业余方法是:


接受字符串->接受或定义不应该在字符串中的字符->运行循环检查字符串的每个字符,获取一个计数器变量,如果变量=0,则打印字符串。

业余方法是:

接受字符串->接受或定义不应该在字符串中的字符->运行循环检查字符串的每个字符,获取一个计数器变量,如果变量=0,则打印字符串