Python 使用re.findall()的结果
我正在使用Pandoc将HTML转换为LaTeX。它工作得很好,但是我想对输出进行后期处理以满足我的需要。考虑以下输出:Python 使用re.findall()的结果,python,regex,Python,Regex,我正在使用Pandoc将HTML转换为LaTeX。它工作得很好,但是我想对输出进行后期处理以满足我的需要。考虑以下输出: string = r'foo\r\nbar\r\n\begin{longtable}[c]{@{}ll@{}}\r\nbar & bar\tabularnewline\r\nbar & bar\r\n\bottomrule\r\n\end{longtable}' 我需要做的是捕获表格的对齐方式(c选项、列配置和表格的内容。以下是我到目前为止所做的工作: ta
string = r'foo\r\nbar\r\n\begin{longtable}[c]{@{}ll@{}}\r\nbar & bar\tabularnewline\r\nbar & bar\r\n\bottomrule\r\n\end{longtable}'
我需要做的是捕获表格的对齐方式(c
选项、列配置和表格的内容。以下是我到目前为止所做的工作:
tabular_setup = re.findall(r'\\begin{longtable}\[(.*)\]{(.*)}(.*)\\end{longtable}', string, re.DOTALL)
if tabular_setup:
tabular_align = tabular_setup[0][0]
column_setup = tabular_setup[0][1]
tab_content = tabular_setup[0][2]
现在我可以将这些值更新为我想要的任何值,但是,如何更新原始字符串中的值呢
def repl(matchobj):
if matchobj.group(1):
return "1" #something
if matchobj.group(2):
return "1" #something
new=re.sub(r"\\begin{longtable}\[(.*)\]{(.*)}(.*)\\end{longtable}",repl,string)
您可以使用
re.sub
和您自己的replacement函数
更新哪些组?谢谢,但是您的repl()
方法接受一个参数,并且您不会在sub()中传递它
call@EddaSnorra它通过了implicitly@EddaSnorra它正在工作。你需要返回一个字符串,我没有在exmaple中提供,因为我不知道你想做什么替换