在C 中带有正则表达式的文本树

parsing tree of text with regex in C++

本文关键字:文本 正则表达式      更新时间:2023-10-16

请帮助!我需要创建用于将以下文本解析到树中的代码/正面。

some text I want to ignore==toplevel==
some text, with newlines maybe
===nextlevel===
more text. This is a child node of 'toplevel'
===anotherchild===
this is a sibling of 'nextlevel' and a child of 'toplevel'====leaf====
this is a child of 'anotherchild'
====leaf2====
sibling of 'leaf' & child of 'anotherchild'
===child3===
this is a sibling of 'anotherchild' and 'nextlevel' and a child of 'toplevel'

等。你明白了。

我无法阻止子级别与高级级别匹配。我尝试过= {2},但是===和====仍然匹配。只需获得一个匹配,即提取所有文本以下" == toplevel =="将是一个开始。我似乎无法忽略/食用新线。

任何帮助!查理。

以下正则符合第一行:"^== [^=]"。也就是说,一条以两个相等的符号开始的线,然后是一个不是平等符号的东西。您可以在其他级别使用类似模式。

newline跳过/处理实际上取决于您如何阅读文本。如果可能的话,我会在输入例程中处理新线,而不是试图将它们与Regexes匹配。

只需匹配所有 =+(name)=+字符串,然后计数代码中的= 的数量。