我在R中使用gsub将文本添加到字符串的中间.它工作得很好,但由于某种原因,当位置太长时,它会抛出错误.代码如下:
gsub(paste0('^(.{', as.integer(loc[1])-1, '})(.+)$'), new_cols, sql)
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)Error in gsub(paste0("^(.{273})(.+)$"), new_cols, sql) : invalid regular expression '^(.{273})(.+)$', reason 'Invalid contents of {}'
当括号中的数字(在这种情况下为273)较小时,此代码可以正常工作,但当它很大时则不行.
这会产生错误:
sql <- "The cat with the bat went to town. He ate the fat mat and wouldn't stop til the sun came up. He was a fat cat that lived with a rat who owned many hats.The cat with the bat went to town. He ate the fat mat and wouldn't stop til the sun came up. …Run Code Online (Sandbox Code Playgroud)