How to replace square brackets with curly brackets using R's regex?

前端 未结 3 553
一生所求
一生所求 2021-01-14 11:59

Due to conversions between pandoc-citeproc and latex I\'d like to replace this

[@Fotheringham1981]

with this

\\cite{Fotheringham1

3条回答
  •  渐次进展
    2021-01-14 12:53

    You need to use capturing group.

    x <- c("[@Fotheringham1981]", "df[1,2]")
    gsub("\\[@([^\\]]*)\\]", "\\\\cite{\\1}", x, perl=T)
    # [1] "\\cite{Fotheringham1981}" "df[1,2]" 
    

    or

    gsub("\\[@(.*?)\\]", "\\\\cite{\\1}", x)
    # [1] "\\cite{Fotheringham1981}" "df[1,2]"
    

提交回复
热议问题