【发布时间】:2021-11-01 08:14:24
【问题描述】:
我正在尝试从 tex 文件中提取所有 Latex 命令,然后将命令转换为 xml 标记。为此,我必须使用 C#。
我目前可以使用这个正则表达式匹配命令
\\(?<command>(?:[^a-zA-Z]|[a-zA-Z]+[*=']?))(?<param>\[.+\])*(?<content>\{.+?}{1,2})
目前可以匹配大多数命令,如\command{foobar}、\command[param]{foobar} 和\command[param]{foobar}{foobar},并将这些匹配替换为<command attr1="param">content</command>,但我在匹配嵌套命令时遇到问题
举例
\firstcommand{\secondcommand{nestedcontent} outercontent} 应替换为 <firstcommand><secondcommand>nestedcontent</secondcommand> outercontent</firstcommand> 但我无法将此模式与我的正则表达式匹配
我最后一次尝试是尝试使用 PCRE.NET 库来使用这个正则表达式 \\(?<command>(?:[^a-zA-Z]|[a-zA-Z]+[*=']?))(?<param>\[.+\])*(\{(?<content>(?:[^{}]*|(?R))+)\}) 进行递归匹配,但是我得到了这个异常 PCRE.PcreMatchException: 'match limit exceeded' 这显然意味着它无法匹配许多嵌套模式。
有什么帮助吗?
【问题讨论】: