R中的正则表达式,用于删除最后一个空格之后的字符串部分 [英] Regular expression in R to remove the part of a string after the last space
本文介绍了R中的正则表达式,用于删除最后一个空格之后的字符串部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在R中有一个gsub
表达式,以删除最后一个空格之后出现的字符串中的所有内容.例如. string="Da Silva UF"
应该还给我"Da Silva"
.有什么想法吗?
I would like to have a gsub
expression in R to remove everything in a string that occurs after the last space. E.g. string="Da Silva UF"
should return me "Da Silva"
. Any thoughts?
推荐答案
您可以使用以下内容.
string <- 'Da Silva UF'
gsub(' \\S*$', '', string)
[1] "Da Silva"
说明:
' '
\S* non-whitespace (all but \n, \r, \t, \f, and " ") (0 or more times)
$ before an optional \n, and the end of the string
这篇关于R中的正则表达式,用于删除最后一个空格之后的字符串部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文