谢谢。
例如:
str = "he@llo wor*ld i'm using state-of-the-art technologies it's i4u"
预期输出" i'm using state-of-the-art technologies it's "
我已经尝试了以下正则表达式。
lines <- c("i'm",
'gas-lighting',
"i'm gas-lighting",
"i-love-you",
"i@u",
"b2b",
"i'm gas-lighting u i@u b2b")gsub("\\w+[^a-z'-]+\\w+", " ", lines)
[1] "i'm" "gas-lighting" "i' -lighting" "i-love-you" " "
" " "i' - "问题是单词之间的间距?尝试跳过空格。
gsub("\\w+[^a-z\\s'-]+\\w+", " ", lines)**
[1] "i'm" "gas-lighting" "i' -lighting" "i-love-you" " "
" " "i' - "它不会跳过空格?应为以下字符串。
[1] "i'm" "gas-lighting" "i'm gas-lighting" "i-love-you" " "
" " "i'm gas-lighting u "更新2:好的,到目前为止一切正常。
> lines <- c("i'm",
+ 'gas-lighting',
+ "i'm gas-lighting",
+ "i-love-you",
+ "i@u",
+ "b2b",
+ "i'm gas-lighting u and you and you i@u b2b",
+ " he@llo wor$ld how*are&you ")
>
> # split a string at spaces then remove the words
> # that contain any non-alphabetic characters (excpet "-", "'")
> # then paste them together (separate them with spaces)
> unlist(lapply(lines, function(line){
+ words <- unlist(strsplit(line, "\\s+"))
+ words <- words[!grepl("[^a-z'-]", words, perl=TRUE)]
+ paste(words, collapse=" ")}))
[1] "i'm" "gas-lighting"
[3] "i'm gas-lighting" "i-love-you"
[5] "" ""
[7] "i'm gas-lighting u and you and you" "" 更新1:到目前为止,我使用的是以下正则表达式。
> # replace word at the beginning of a string
> lines <- gsub("^\\s*\\w*[^a-z'-]+\\w*", " ", lines); lines
[1] "i'm" "gas-lighting" "i'm gas-lighting" "i-love-you"
[5] " " " " "i'm gas-lighting u i@u "
> # replace word at the end of a string
> lines <- gsub("\\s[a-z]+[^a-z'-]+\\w*$", " ", lines); lines
[1] "i'm" "gas-lighting" "i'm gas-lighting" "i-love-you"
[5] " " " " "i'm gas-lighting u i@u "
> # replace words between spaces
> gsub("\\s\\w*[^a-z'-]+\\w*\\s", " ", lines)
[1] "i'm" "gas-lighting" "i'm gas-lighting" "i-love-you" " "
[6] " " "i'm gas-lighting u "发布于 2019-01-27 11:29:17
我想出了一个间接的方法,但它起作用了。
library(tidyverse)
str = "he@llo wor*ld i'm using state-of-the-art technologies it's i4u"
##Break the string based on spaces
break_1 <- (str_split(str, pattern = "\\s"))
##Find the good words and put them in a vector
good_words <- unlist(break_1)[!sapply(break_1,
function(i)str_detect(i,pattern = "[^(Aa-zZ|\\-|')]"))]
##Merge the vector
merged_vector <- paste0(good_words, collapse = " ")
merged_vector发布于 2019-01-27 14:15:59
作为Harro Cyranka with grepl的变体
paste0(sapply(break_1, function(x) x[!grepl("[^Aa-zZ|'|-]", x)]), collapse = " ")https://stackoverflow.com/questions/54384744
复制相似问题