This question already has an answer here:
这个问题在这里已有答案:
- Regex; eliminate all punctuation except 2 answers
- 正则表达式;消除除2个答案之外的所有标点符号
- strsplit on all spaces and punctuation except apostrophes [duplicate] 1 answer
- strsplit所有空格和标点符号除了撇号[重复] 1个答案
I have a string like
我有一个字符串
a <- "Hi. I m cool, but I need help!"
And as an output i would like to have
作为我想要的输出
"hi" "." "I" "m" "cool" "," "but" "I" "need" "help" "!"
Furthermore I don't want to use extra packages.
此外,我不想使用额外的包。
1 个解决方案
#1
1
We can use strsplit
我们可以使用strsplit
a1 <- strsplit(a, '\\s|(?=[!,.])\\s*', perl = TRUE)[[1]]
a1[nzchar(a1)]
#[1] "Hi" "." "I" "m" "cool" "," "but" "I" "need" "help" "!"
#1
1
We can use strsplit
我们可以使用strsplit
a1 <- strsplit(a, '\\s|(?=[!,.])\\s*', perl = TRUE)[[1]]
a1[nzchar(a1)]
#[1] "Hi" "." "I" "m" "cool" "," "but" "I" "need" "help" "!"