读取文本配置文件:使用正则表达式进行解析
寻找一种使用多行正则表达式匹配器读取以下配置文件示例的方法。我可以只按行读取文件,但我想了解灵活的正则表达式匹配的细节。
因此配置文件中充满了如下代码块:
blockName BLOCK
IDENTIFIER value
IDENTIFIER value
IDENTIFIER
"string literal value that
could span multiple lines"
标识符的数量可以从 1..无穷大。标识符可以是名称、描述、类型等。
我以前从未使用过多行正则表达式。我对这个过程不是很熟悉。我本质上想使用 findAll 函数,使用此正则表达式将所有解析的块数据放入数据结构中进行处理。
编辑:澄清:我只想读这个文件一次。我不关心效率或优雅。我想将信息读入数据结构,然后以不同的格式吐出。这是一个很大的文件(3000 行),我不想手动执行此操作。
Looking for a way to read the following config file sample using a multi line regex matcher. I could just read in the file by line, but I want to get decent with the specifics of flexible regular expression matching.
So the config file is filled with blocks of code as follows:
blockName BLOCK
IDENTIFIER value
IDENTIFIER value
IDENTIFIER
"string literal value that
could span multiple lines"
The number of identifiers could be from 1..infinity. IDENTIFIER could be NAME, DESCRIPTION, TYPE, or the like.
I have never worked with multi line regular expressions before. I'm not very familiar with the process. I essentially want to use a findAll function using this regular expression to put all of the parsed block data into a data structure for processing.
EDIT: clarification: I'm only looking to read this file once. I do not care about efficiency or elegance. I want to read the information into a data structure and then spit it out in a different format. It is a large file (3000 lines) and I don't want to do this by hand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为正则表达式是最好的工具。
I don't think regex is the best tool for this.
试试这个,它应该在 perl 正则表达式中工作:
我使用以下测试文本在 REGex TESTER 验证了它:
它将仅当文件以换行符结尾时才查找最后一个块/标识符
Try this, which should work in perl regular expressions:
I verified it at REGex TESTER using the following test text:
It will only find the last block/identifier if the file ends in a newline