从 R 中的字符串中删除指定的模式 [英] Remove specified pattern from string in R
本文介绍了从 R 中的字符串中删除指定的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像下面这样的字符串
I have a string like the following
s <- "abc a%bc 1.2% 234 1.2 (1.4%)) %3ed"
我想删除所有带有 %.所以结果是
I would like to remove all "words" which have the %. So the result would be
"abc 234 1.2"
推荐答案
可以使用
> gsub("^\\s+|\\s+$", "", (gsub("\\s+", " " ,gsub("\\s+\\S*%\\S*(?=\\s+|$)", " ",input, perl=TRUE))))
#[1] "abc 234 1.2"
代码分解
gsub("^\\s+|\\s+$", "", (gsub("\\s+", " " ,gsub("\\s+\\S*%\\S*(?=\\s+|$)", " ",input, perl=TRUE))))
<--------------------------------------------------->
Remove strings with %
<------------------------------------------------------------------------>
Substitute extra spaces with single space from resultant string obtained from above
<-------------------------------------------------------------------------------------------------->
Trim initial and final whitespaces from the string obtained from above
正则表达式分解
\\s+ #Match whitespaces
\\S* #Match all non whitespace character before % if its there
% #Match % literally
\\S* #Match all non whitespace character after % if its there
(?=\\s+|$) #Lookahead to check whether there is a space or end of string after matching word with %
这篇关于从 R 中的字符串中删除指定的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文