删除R中单词开头的特殊字符 [英] Removing special characters in the beginning of a word in R
本文介绍了删除R中单词开头的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码从单词开头删除特殊字符:
I am using the following code to remove the special characters from the begining of a word:
>gsub("^[^[:alnum:]]",'','#C++')
[1] "C++"
但是如果开始时有多个特殊字符,它将仅删除第一个特殊字符:
But If there are multiple special characters in the beggining it removes only the first one:
>gsub("^[^[:alnum:]]",'','$#C++')
[1] "#C++"
如何使开头的所有特殊字符都删除,所以输出应为"C++"
?
How can I make it to remove all the special characters in the begining so the output should be "C++"
?
推荐答案
我们从字符串(^
)的开头匹配一个或多个非字母数字字符([^[:alnum:]]+
),并将其替换为''
We match one or more non-alpha numeric characters ([^[:alnum:]]+
) from the beginning of the string (^
) and replace it with ''
.
sub("^[^[:alnum:]]+",'','$#C++')
或使用
sub("^\\W+",'','$#C++')
#[1] "C++"
这篇关于删除R中单词开头的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文