在R的字符串中的两个字母之间添加空格 [英] Add space between two letters in a string in R
本文介绍了在R的字符串中的两个字母之间添加空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个像这样的字符串
Suppose I have a string like
s = "PleaseAddSpacesBetweenTheseWords"
我如何在R中使用gsub在单词之间添加一个空格,以便获得
How do I use gsub in R add a space between the words so that I get
"Please Add Spaces Between These Words"
我应该做类似的事情
gsub("[a-z][A-Z]", ???, s)
我要做什么??.另外,我发现R的正则表达式文档令人困惑,因此在R中对正则表达式的引用或撰写将不胜感激.
What do I put for ???. Also, I find the regular expression documentation for R confusing so a reference or writeup on regular expressions in R would be much appreciated.
推荐答案
您只需要捕获匹配项,然后使用\1
语法引用捕获的匹配项即可.例如
You just need to capture the matches then use the \1
syntax to refer to the captured matches. For example
s = "PleaseAddSpacesBetweenTheseWords"
gsub("([a-z])([A-Z])", "\\1 \\2", s)
# [1] "Please Add Spaces Between These Words"
当然,这只是在每个小写/大写字母配对之间放置了一个空格.它不知道什么是真正的单词".
Of course, this just puts a space between each lower-case/upper-case letter pairings. It doesn't know what a real "word" is.
这篇关于在R的字符串中的两个字母之间添加空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文