Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gsub every other occurrence of a condition

Tags:

regex

r

Sometimes I use R for parsing text from pdfs for quotes in writing an article (I use LATEX). One thing I'd like to do is change straight left and right quotes to LATEX style left and right quotes.

LATEX would change "dog" to ``dog'' (so two ` for the left and two ' for the right)

Here's an example of what I have and what I'd like to get.

#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''."

EDIT: Thought I'd share the actual use for context. Using ttmaccer's solution (works on a windows machine):

g <- function(){
    require(qdap)
    x <- readClipboard()
    x <- clean(paste2(x, " "))
    zz <- mgsub(c("- ", "“", "”"), c("", "``", "''"), x)
    zz <- gsub("\"([^\"].*?)\"","``\\1''", zz)
    writeClipboard(noquote(zz), format = 1)
}

Note: qdap can be downloaded HERE

like image 920
Tyler Rinker Avatar asked Sep 03 '25 09:09

Tyler Rinker


1 Answers

A naive solution would be:

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

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

but I'm not sure how you would handle "some \"text\" with one \""

like image 131
shhhhimhuntingrabbits Avatar answered Sep 05 '25 01:09

shhhhimhuntingrabbits