我有一个文本文件,结构如下:
segmentA {
content Aa
content Ab
content Ac
....
}
segmentB {
content Ba
content Bb
content Bc
......
}
segmentC {
content Ca
content Cb
content Cc
......
}
我知道如何在整个文本文件中搜索certrain字符串,但如何定义搜索某个字符串,例如“segmentC”。我需要像reg表达式这样的东西来告诉脚本??地址:
如果文本以“segmentC{”开头,则对某个字符串执行搜索,直到出现第一个“}”。你知道吗
有人有主意吗?你知道吗
提前谢谢!你知道吗
不是正则表达式的解决方案…但会做的工作!你知道吗
根据您所寻找的复杂性,您可以从具有基于行的模式搜索的简单状态机到完整的lexer。你知道吗
基于行的搜索
下面的示例假设您只查找一个段,并且
segmentC {
和结束}
在一行上。你知道吗简单Lexer
如果需要更大的灵活性,可以设计一个简单的lexer/parser耦合。例如,下面的代码不假设行之间语法的组织。它也会忽略未知模式,而典型的lexer则不会(通常会引发语法错误):
全Lexer
如果您需要更大的灵活性和可重用性,就必须创建一个完整的解析器。不需要重新发明轮子,看看this list of language parsing modules,你可能会找到一个适合你的。你知道吗
相关问题 更多 >
编程相关推荐