我必须根据符号和单词提取R中字符串的部分。我有一个名称,如用符号和单词拆分字符串
s <-"++can+you+please-help +me"
和输出将是:
"+ can" "+you" "+please" "-help" "+me"
其中与对应的符号的所有单词之前被示出。我试过使用strsplit和sub函数,但是我正在努力获得我想要的输出。你能帮我么?谢谢!
我必须根据符号和单词提取R中字符串的部分。我有一个名称,如用符号和单词拆分字符串
s <-"++can+you+please-help +me"
和输出将是:
"+ can" "+you" "+please" "-help" "+me"
其中与对应的符号的所有单词之前被示出。我试过使用strsplit和sub函数,但是我正在努力获得我想要的输出。你能帮我么?谢谢!
做
library(stringi)
result = unlist(stri_match_all(regex = "\\W\\w+",str = s))
结果
> result
[1] "+can" "+you" "+please" "-help" "+me"
无符号
如果你只想要的话(无符号),这样做:
result = unlist(stri_match_all(regex = "\\w+",str = s))
result
[1] "can" "you" "please" "help" "me"
下面是使用base R
regmatches(s, gregexpr("[[:punct:]]\\w+", s))[[1]]
#[1] "+can" "+you" "+please" "-help" "+me"
谢谢!有用。有没有什么网站可以提供一个很好的例子,说明如何编写我在R中寻找的模式? –
请分享你为了不重复同样的试了一下一个选项。 –
https://stackoverflow.com/questions/15573887/split-string-with-regex – Olivia
为什么有''+ can“'的空间。您是否打算删除其中一个加号并将其替换为该空格? 'unlist(strsplit(s,split =“(?<= \\ w)\\ s *(?= [+ - ] +)”,perl = T))'非常接近。 – Abdou