我在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. He was a fat cat that lived with a rat who owned many hats."
new_cols <- "happy"
gsub('^(.{125})(.+)$', new_cols, sql) #**Works
gsub('^(.{273})(.+)$', new_cols, sql)
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)Error in gsub("^(.{273})(.+)$", new_cols, sql) : invalid regular expression '^(.{273})(.+)$', reason 'Invalid contents of {}'
Wik*_*żew 13
R gsub默认使用TRE正则表达式库.限制量词中的边界从0开始有效,直到RE_DUP_MAX在TRE代码中定义.看到这个TRE参考:
甲界是以下内容,其中的一个
n和m是之间的无符号十进制整数0和RE_DUP_MAX
似乎RE_DUP_MAX设置为255(参见此TRE源文件显示#define RE_DUP_MAX 255),因此,您不能在{n,m}限制量词中使用更多.
使用PCRE正则表达式风味,添加perl = TRUE它将起作用.
R演示:
> 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. He was a fat cat that lived with a rat who owned many hats."
> new_cols <- "happy"
> gsub('^(.{273})(.+)$', new_cols, sql, perl=TRUE)
[1] "happy"
Run Code Online (Sandbox Code Playgroud)