使用正则表达式查找(并替换)LaTeX表中的第n列

时间:2012-07-26 12:31:39

标签: regex r

我有一个LaTeX表的字符串。我试图找到第n个(比方说第三个)列并将所有内容包装在内,比如说\emph{}而不匹配分隔符号。

我正在寻找第二列的第一个&...&。然后找到下一个&...&,它是第二个分组,并且不是巧合,是表格中的第三列。

我的虚拟示例有效,但有点不同,因为它有两个&...&之间的文本。我将在稍后阶段解决一些问题 - 我需要使用后退和前向引用将&置于\emph{}调用之外。

xy <-  "This is &more or less& a match and here is &another one&.\nSecond line with &occurrance 1& and &occurrance 2&"
gsub("(&.*?&)|(.*?&)(.*)(&.*?&)", "\\1\\2\\3\\\\emph{\\4}", xy, perl = TRUE)
[1] "This is &more or less& a match and here is \\emph{&another one&}.\nSecond line with &occurrance 1& and \\emph{&occurrance 2&}"

当我用LaTeX表(bam!)将读数设置为一个缺口时,它有点不同。两个&...&之间没有字符,这意味着一个&与两列相邻。考虑到这一点,我删除了(.*)。无论我尝试什么,我都无法让它发挥作用。有什么提示吗?

library(xtable)
data(tli)
tli.table <- xtable(tli[1:5,])
x <- print.xtable(tli.table, print.results = FALSE, include.rownames = FALSE)

cat(x)
% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Thu Jul 26 14:13:39 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlllr}
  \hline
grade & sex & disadvg & ethnicty & tlimth \\ 
  \hline
  6 & M & YES & HISPANIC &  43 \\ 
    7 & M & NO & BLACK &  88 \\ 
    5 & F & YES & HISPANIC &  34 \\ 
    3 & M & YES & HISPANIC &  65 \\ 
    8 & M & YES & WHITE &  75 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

gsub("(&.*?&)(&.*?&)", "\\1\\\\emph{\\2}", x, perl = TRUE)

2 个答案:

答案 0 :(得分:4)

假设1 st 列为n <- 1(而不是n <- 0),则应该用于替换第n列的正则表达式应该是:

(?m)^(?=[^&\n\r]*&)((?:[^&]*&){n-1})\\s*([^&]*?)\\s*(&|\\\\)
                                ↑
                                └ replace this n-1 with real number

然后替换字符串必须是\\1\\\\emph{\\2}\\3

所以你的替换代码是:

input <- "% latex table generated in R 2.15.1 by xtable 1.7-0 package\n% Thu Jul 26 17:49:09 2012\n\\begin{table}[ht]\n\\begin{center}\n\\begin{tabular}{rlllr}\n  \\hline\ngrade & sex & disadvg & ethnicty & tlimth \\\\ \n  \\hline\n  6 & M & YES & HISPANIC &  43 \\\\ \n    7 & M & NO & BLACK &  88 \\\\ \n    5 & F & YES & HISPANIC &  34 \\\\ \n    3 & M & YES & HISPANIC &  65 \\\\ \n    8 & M & YES & WHITE &  75 \\\\ \n   \\hline\n\\end{tabular}\n\\end{center}\n\\end{table}\n"

n <- 1
regex <- paste(c('(?m)^(?=[^&\n\r]*&)((?:[^&]*&){', n-1, '})\\s*([^&]*?)\\s*(&|\\\\)'), collapse='')
cat(gsub(regex, "\\1\\\\emph{\\2}\\3", input, perl = TRUE))

答案 1 :(得分:2)

另一种方法是在调用xtable:

之前将列包装在emph{}
data(tli)
tli[, 4] <- paste0("\\\\emph{", tli[, 4], "}")

然后你的脚本就像你拥有它一样:

tli.table <- xtable(tli[1:5,])
x <- print.xtable(tli.table, print.results = FALSE, include.rownames = FALSE)
cat(x)

产生以下内容,这应该会产生预期的结果:

% latex table generated in R 2.15.0 by xtable 1.7-0 package
% Thu Jul 26 16:08:58 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlllr}
  \hline
grade & sex & disadvg & ethnicty & tlimth \\ 
  \hline
  6 & M & YES & $\backslash$$\backslash$emph\{HISPANIC\} &  43 \\ 
    7 & M & NO & $\backslash$$\backslash$emph\{BLACK\} &  88 \\ 
    5 & F & YES & $\backslash$$\backslash$emph\{HISPANIC\} &  34 \\ 
    3 & M & YES & $\backslash$$\backslash$emph\{HISPANIC\} &  65 \\ 
    8 & M & YES & $\backslash$$\backslash$emph\{WHITE\} &  75 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}