alki
alki

Reputation: 3574

R regex - replacing nth match

Suppose you want to replace the second string match with the second element in the vector replacement. For instance:

x <- "CBCADEFGHI"
pattern <- "(A|D|C)"
replacement <- c("X","Y","Z")

How would you go about replacing only the second pattern match, which is "C" because its the second pattern that is discovered, with the corresponding replacement vector element "Z"?

Desired Output:

"CBZADEFGHI"

Upvotes: 3

Views: 2190

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99331

Hopefully I understand this correctly. Here's my idea.

## find the position of the second match
g <- gregexpr(pattern, x)[[1]][2]
## get the individual letter elements out of 'pattern'
s <- scan(text = gsub("[()]", "", pattern), sep = "|", what = "")
## replace the position 'g' in 'x' with the matched element in 'replacement'
substr(x, g, g) <- replacement[match(substr(x, g, g), s)]
## resulting in
x
# [1] "CBZADEFGHI"

Upvotes: 8

Related Questions