I would like to insert substring "A"
into a string of length > 65536 at position n
. I am using gsub to do this since it is very efficient.
n = 5
text <- paste(rep("a", 70000), collapse = "")
lhs <- '^(.{n})(.*)$'
rhs <- '\\1A\\2'
gsub(lhs, rhs, text, perl=TRUE)
When I choose n to be small, e.g. 5, the code works fine. If however n = 66000
the following error message shows:
In gsub(lhs, rhs, text, perl = TRUE) : PCRE pattern compilation error
'number too big in {} quantifier'
at '})(.*)$'
How can I solve this problem?
CodePudding user response:
It looks like you won't be able to use sub
here due to pattern length restrictions. But actually, for this problem the base string functions should work and probably would be more efficient anyway:
insertA <- function(x, n) {
return(paste0(substring(x, 1, n-1), "A", substring(x, n, nchar(x))))
}
text <- paste(rep("a", 30), collapse = "")
insertA(text, 5)
[1] "aaaaAaaaaaaaaaaaaaaaaaaaaaaaaaa"
This approach simply uses substring
along with paste
to split the A
character in between whatever two halves the user chooses.