Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex gsub每发生一次情况_Regex_R - Fatal编程技术网

Regex gsub每发生一次情况

Regex gsub每发生一次情况,regex,r,Regex,R,有时我使用R来解析PDF中的文本,以便在撰写文章时引用(我使用LATEX)。我想做的一件事是将直接的左引号和右引号更改为LATEX样式的左引号和右引号 LATEX将“dog”改为“dog”(左两个,右两个) 这是一个我所拥有和想要得到的例子 #currently x <- c('I like "proper" cooking.', 'I heard him say, "I want some too" and "nice".') [1] "I like \"proper\" cookin

有时我使用R来解析PDF中的文本,以便在撰写文章时引用(我使用LATEX)。我想做的一件事是将直接的左引号和右引号更改为LATEX样式的左引号和右引号

LATEX将“dog”改为“dog”(左两个,右两个)

这是一个我所拥有和想要得到的例子

#currently
x <- c('I like "proper" cooking.', 'I heard him say, "I want some too" and "nice".')

[1] "I like \"proper\" cooking."   "I heard him say, \"I want some too\" and \"nice\"."

#desired outcome
[1] "I like ``proper'' cooking."   "I heard him say, ``I want some too'' and ``nice''."
#当前
xa两阶段溶液:

阶段1:使用
“(((?:[^\\”]|\\)*)”
匹配双引号字符串

第2阶段:使用
\\”([^\\“]*)\\“
替换第1阶段第1组的
\”
,最简单的解决方案是:

> gsub("\"([^\"].*?)\"","``\\1''",x)

[1] "I like ``proper'' cooking."                        
[2] "I heard him say, ``I want some too'' and ``nice''."

但我不确定你会如何处理一些文本,而其中一个文本是有效的。无论如何,我都会检查输出,所以我只是希望它大部分时间都能工作(节省时间)。
> gsub("\"([^\"].*?)\"","``\\1''",x)

[1] "I like ``proper'' cooking."                        
[2] "I heard him say, ``I want some too'' and ``nice''."