删除R中字符串的中间部分 [英] Remove middle part of string in R
问题描述
如何去除刺的中间部分?例如,取字符串 - '2018_002.Feb'.对于此示例,我想删除002.",以便获得2018_Feb"
How do I remove the middle part of a sting? For example, take the string - '2018_002.Feb'. For this example, I want to remove '002.', so that I get '2018_Feb'
谁能帮帮我?谢谢!
推荐答案
我喜欢使用 stringr
包而不是基本的 r 包来处理字符串,因为我发现函数的语法更多持续的.
I like to use the stringr
package as opposed to the base r packages for string manipulations because I find the syntax for the functions more consistent.
library(stringr)
var = "2018_002.Feb"
str_replace(var, pattern = "_\\d+\\.", replacement = "_")
# [1] "2018_Feb"
使用str_replace()
,您基本上是在字符串中搜索模式并将其替换为其他内容.通常替换将只是一个空的 ""
,但在这种情况下,在函数找到 _
字符的地方开始搜索更容易,因为它相当独特.从那里开始,您希望匹配该期间之后的所有数字.
With the str_replace()
you are basically searching a pattern in the string an replacing it with something else. Often the replacement will just be an empty ""
, but in this case, it is easier to start the search where the function finds a _
character because it is rather unique. From there you want to match on all the numbers that come after up to the period.
我建议学习一些关于正则表达式的知识.R中的基本正则表达式备忘单是一个很好的资源.
I recommend learning a bit about regular expression. The Basic Regular Expressions in R Cheat Sheet is a good resource.
这个问题的正则表达式是这样的:
The regex for this problem reads something like this:
- 首先找到
_
后跟数字\\d
的字符并保持匹配数字+
直到您到达句点\\.
- 找到匹配项
_002.
"后,将其替换为_
"
- first find
_
character that is followed by a number\\d
and keep matching numbers+
until you reach a period\\.
- Once you find this match "
_002.
", replace it with "_
"
希望这是可以理解的!
这篇关于删除R中字符串的中间部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!