删除包含标点符号(R)的字符串中的所有单词

删除包含标点符号(R)的字符串中的所有单词,r,regex,string,gsub,R,Regex,String,Gsub,在R中,如何删除包含标点符号的字符串中的任何单词,使单词不带标点符号 test.string <- "I am:% a test+ to& see if-* your# fun/ction works o\r not" desired <- "I a see works not" 你可以使用这个正则表达式 (?<=\\s|^)[a-z0-9]+(?=\\s|$) 你可以使用这个正则表达式 (?<=\\s|^)[a-z0-9]+(?=\\s|$) 以

在R中,如何删除包含标点符号的字符串中的任何单词,使单词不带标点符号

  test.string <- "I am:% a test+ to& see if-* your# fun/ction works o\r not"

  desired <- "I a see works not"
你可以使用这个正则表达式

(?<=\\s|^)[a-z0-9]+(?=\\s|$)
你可以使用这个正则表达式

(?<=\\s|^)[a-z0-9]+(?=\\s|$)
以下是一种使用sub的方法,似乎有效:

test.string <- "I am:% a test$ to& see if* your# fun/ction works o\r not"
gsub("[A-Za-z]*[^A-Za-z ]\\S*\\s*", "", test.string)

[1] "I a see works not"
然后,我们将其替换为空字符串,以删除其中包含一个或多个符号的单词。

下面是一种使用sub的方法,该方法似乎有效:

test.string <- "I am:% a test$ to& see if* your# fun/ction works o\r not"
gsub("[A-Za-z]*[^A-Za-z ]\\S*\\s*", "", test.string)

[1] "I a see works not"

然后,我们用空字符串替换,以删除包含一个或多个符号的单词。

这里有两种方法

第一种方法:

str_split(test.string, " ", n=Inf) %>%  # spliting the line into words
unlist %>% 
.[!str_detect(., "\\W|\r")] %>%         # detect words without punctuation or \r
paste(.,collapse=" ")                   # collapse the words to get the line
str_extract_all(test.string, "^\\w+|\\s\\w+\\s|\\w+$") %>% 
unlist %>% 
trimws() %>% 
paste(., collapse=" ")
第二种方法:

str_split(test.string, " ", n=Inf) %>%  # spliting the line into words
unlist %>% 
.[!str_detect(., "\\W|\r")] %>%         # detect words without punctuation or \r
paste(.,collapse=" ")                   # collapse the words to get the line
str_extract_all(test.string, "^\\w+|\\s\\w+\\s|\\w+$") %>% 
unlist %>% 
trimws() %>% 
paste(., collapse=" ")
^\\w+-仅具有[a-zA-Z0-9_]且也是字符串开头的单词 \\s\\w+\\s-带[a-zA-Z0-9_]且在单词前后有空格的单词 \\w+$-具有[a-zA-Z0-9_]且也是字符串结尾的单词
这里有更多的方法

第一种方法:

str_split(test.string, " ", n=Inf) %>%  # spliting the line into words
unlist %>% 
.[!str_detect(., "\\W|\r")] %>%         # detect words without punctuation or \r
paste(.,collapse=" ")                   # collapse the words to get the line
str_extract_all(test.string, "^\\w+|\\s\\w+\\s|\\w+$") %>% 
unlist %>% 
trimws() %>% 
paste(., collapse=" ")
第二种方法:

str_split(test.string, " ", n=Inf) %>%  # spliting the line into words
unlist %>% 
.[!str_detect(., "\\W|\r")] %>%         # detect words without punctuation or \r
paste(.,collapse=" ")                   # collapse the words to get the line
str_extract_all(test.string, "^\\w+|\\s\\w+\\s|\\w+$") %>% 
unlist %>% 
trimws() %>% 
paste(., collapse=" ")
^\\w+-仅具有[a-zA-Z0-9_]且也是字符串开头的单词 \\s\\w+\\s-带[a-zA-Z0-9_]且在单词前后有空格的单词 \\w+$-具有[a-zA-Z0-9_]且也是字符串结尾的单词
你需要逃离这个世界\\s@TimBiegeleisen解释将帮助我学习新事物,如果你不介意的话,你能解释一下你的第一个评论吗?@CodeManiac我编辑了你的答案,使其在R中工作。不确定我们的答案中哪一个会更好。@TimBiegeleisen ohh我知道你是想针对R,无论如何感谢编辑,我对R没有任何了解,因为我对这个行业非常陌生,但我知道regex是一种跨平台的语言,大多数语言都支持它,但有一些限制,所以我添加了这个答案OK,但为了将来参考,如果这个问题用特定的编程语言标记,那么OP和将来可能阅读它的任何人都可能期望用这种实际语言找到一个有效的解决方案。但是,你的答案是正确的,因为你需要逃避现实\\s@TimBiegeleisen如果你不介意的话,解释能帮我学到新东西,你能解释一下你的第一个评论吗?@CodeManiac我编辑了你的答案,使其在R中工作。不确定我们的答案中哪一个会更好。@TimBiegeleisen ohh我知道你想针对R,无论如何感谢编辑,我对R没有任何知识,因为我对这个行业非常陌生,但我知道regex有点跨平台,大多数语言都支持它,但有一些局限性,所以我添加了这个答案OK,但为了将来参考,如果这个问题用特定的编程语言标记,那么OP和将来可能阅读它的任何人都可能期望用这种实际语言找到一个有效的解决方案。但是,你的答案是正确的,因为。