stringr:我想删除两侧各有一个空格的游离字符(最多两个)
我很难用stringr实现以下目标:我想删除两边都有空格的散乱字符(最多两个)。我不能让stringr在没有错误的情况下给我好的结果。下面是两个例子。先谢谢你stringr:我想删除两侧各有一个空格的游离字符(最多两个),r,regex,stringr,R,Regex,Stringr,我很难用stringr实现以下目标:我想删除两边都有空格的散乱字符(最多两个)。我不能让stringr在没有错误的情况下给我好的结果。下面是两个例子。先谢谢你 string1<-'john a smith' output1<-'john smith' string2<-'betty ao smith' output2<-'betty smith' string1gsub(\\S{1,2}),“”,c('johna smith','betty ao smith')) #[
string1<-'john a smith'
output1<-'john smith'
string2<-'betty ao smith'
output2<-'betty smith'
string1gsub(\\S{1,2}),“”,c('johna smith','betty ao smith'))
#[1]“约翰·史密斯”“贝蒂·史密斯”
\\S
是非空格字符
{.}
允许重复前面的模式;例如
{2,}
至少2个
{,3}
不超过3
{1,2}
介于1和2之间
在stringr
中也一样,因为它是“just regex”:-)
stringr::str_replace(c('johna smith','betty ao smith'),“\\S{1,2},”)
谢谢。这很简单。可能有其他选择,但我很难想到我需要一个不同的解决方案。几分钟后就可以接受了。