Tyler Rinker
Tyler Rinker

Reputation: 109844

regex grab from beginning to n occurrence of character

I'm really putting time into learning regex and I'm playing with different toy scenarios. One setup I can't get to work is to grab from the beginning of a string to n occurrence of a character where n > 1.

Here I can grab from the beginning of the string to the first underscore but I can't generalize this to the second or third underscore.

x <- c("a_b_c_d", "1_2_3_4", "<_?_._:")

gsub("_.*$", "", x)

Here's what I'm trying to achieve with regex. (`sub`/`gsub`):

## > sapply(lapply(strsplit(x, "_"), "[", 1:2), paste, collapse="_")
## [1] "a_b" "1_2" "<_?"

#or

## > sapply(lapply(strsplit(x, "_"), "[", 1:3), paste, collapse="_")
## [1] "a_b_c" "1_2_3" "<_?_."

Related post: regex from first character to the end of the string

Upvotes: 7

Views: 434

Answers (5)

Justin
Justin

Reputation: 43255

How about:

gsub('^(.+_.+?).*$', '\\1', x)
# [1] "a_b" "1_2" "<_?"

Alternatively you can use {} to indicate the number of repeats...

sub('((.+_){1}.+?).*$', '\\1', x)  # {0} will give "a", {1} - "a_b", {2} - "a_b_c" and so on

So you don't have to repeat yourself if you wanted to match the nth one...

Upvotes: 3

Tyler Rinker
Tyler Rinker

Reputation: 109844

Using Justin's approach this was what I devised:

beg2char <- function(text, char = " ", noc = 1, include = FALSE) {
    inc <- ifelse(include, char, "?")
    specchar <- c(".", "|", "(", ")", "[", "{", "^", "$", "*", "+", "?")
    if(char %in% specchar) {
        char <- paste0("\\", char)
    }
    ins <- paste(rep(paste0(char, ".+"), noc - 1), collapse="")
    rep <- paste0("^(.+", ins, inc, ").*$")
    gsub(rep, "\\1", text)
}

x <- c("a_b_c_d", "1_2_3_4", "<_?_._:")
beg2char(x, "_", 1)
beg2char(x, "_", 2)
beg2char(x, "_", 3)
beg2char(x, "_", 4)
beg2char(x, "_", 3, include=TRUE)

Upvotes: 1

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162321

Here's a start. To make this safe for general use, you'll need it to properly escape regular expressions' special characters:

x <- c("a_b_c_d", "1_2_3_4", "<_?_._:", "", "abcd", "____abcd")

matchToNth <- function(char, n) {
    others <- paste0("[^", char, "]*") ## matches "[^_]*" if char is "_"
    mainPat <- paste0(c(rep(c(others, char), n-1), others), collapse="")
    paste0("(^", mainPat, ")", "(.*$)")
}

gsub(matchToNth("_", 2), "\\1", x)
# [1] "a_b"  "1_2"  "<_?"  ""     "abcd" "_" 

gsub(matchToNth("_", 3), "\\1", x)
# [1] "a_b_c" "1_2_3" "<_?_." ""      "abcd"  "__"   

Upvotes: 5

CHP
CHP

Reputation: 17189

Maybe something like this

x
## [1] "a_b_c_d" "1_2_3_4" "<_?_._:"

gsub("(.*)_", "\\1", regmatches(x, regexpr("([^_]*_){1}", x)))
## [1] "a" "1" "<"

gsub("(.*)_", "\\1", regmatches(x, regexpr("([^_]*_){2}", x)))
## [1] "a_b" "1_2" "<_?"

gsub("(.*)_", "\\1", regmatches(x, regexpr("([^_]*_){3}", x)))
## [1] "a_b_c" "1_2_3" "<_?_."

Upvotes: 1

ennuikiller
ennuikiller

Reputation: 46965

second underscore in perl style regex:

/^(.?_.?_)/

and third:

/^(.*?_.*?_.*?_)/

Upvotes: 1

Related Questions