在第一次/第 n 次出现分隔符时拆分 [英] Split on first/nth occurrence of delimiter

查看:53
本文介绍了在第一次/第 n 次出现分隔符时拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一些我认为很容易的事情.我正在寻找一个单一的正则表达式解决方案(尽管为了完整性欢迎其他解决方案).我想分割 n 次出现的分隔符.

I am trying something I thought would be easy. I'm looking for a single regex solution (though others are welcomed for completeness). I want to split on n occurrences of a delimiter.

这是一些数据:

x <- "I like_to see_how_too"
pat <- "_"

预期结果

假设我想在第一次出现 _ 时拆分:

[1] "I like"  "to see_how_too"

假设我想在第二次出现 _ 时拆分:

[1] "I like_to see"   "how_too"

理想情况下,如果解决方案是正则表达式,则可以推广到第 n 次出现;该解决方案将使用带有单个正则表达式的 strsplit.

Ideally, if the solution is a regex one liner generalizable to nth occurrence; the solution will use strsplit with a single regex.

这是一个不适合我的与 strsplit

Here's a solution that doesn't fit my parameters of single regex that works with strsplit

x <- "I like_to see_how_too"
y <- "_"
n <- 1
loc <- gregexpr("_", x)[[1]][n]

c(substr(x, 1, loc-1), substr(x, loc + 1, nchar(x)))

推荐答案

这是另一个使用 gsubfn 包和一些正则表达式的解决方案.要更改分隔符的 nth 次出现,您可以简单地交换位于范围量词内的数字 — {n}.

Here is another solution using the gsubfn package and some regex-fu. To change the nth occurrence of the delimiter, you can simply swap the number that is placed inside of the range quantifier — {n}.

library(gsubfn)
x <- 'I like_to see_how_too'
strapply(x, '((?:[^_]*_){1})(.*)', c, simplify =~ sub('_$', '', x))
# [1] "I like"  "to see_how_too"

如果您希望第 n 次出现用户定义,您可以使用以下内容:

If you would like the nth occurrence to be user defined, you could use the following:

n <- 2
re <- paste0('((?:[^_]*_){',n,'})(.*)')
strapply(x, re, c, simplify =~ sub('_$', '', x))
# [1] "I like_to see" "how_too" 

这篇关于在第一次/第 n 次出现分隔符时拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆