读取文本配置文件:使用正则表达式进行解析

发布于 2024-11-19 07:17:52 字数 483 浏览 2 评论 0原文

寻找一种使用多行正则表达式匹配器读取以下配置文件示例的方法。我可以只按行读取文件,但我想了解灵活的正则表达式匹配的细节。

因此配置文件中充满了如下代码块:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記憶穿過時間隧道 2024-11-26 07:17:52

我不认为正则表达式是最好的工具。

I don't think regex is the best tool for this.

莫相离 2024-11-26 07:17:52

试试这个,它应该在 perl 正则表达式中工作:

([\w\d]*)\s+BLOCK\s*\n(\s*(NAME|DESCRIPTION|TYPE|...)\s*([\w\d]*|"(.*)")\s*\n)+

我使用以下测试文本在 REGex TESTER 验证了它:

blockName BLOCK
     NAME value
     NAME value
     DESCRIPTION
          "string literal value that
          could span multiple lines"
otherName BLOCK
     NAME value
     TYPE value
     DESCRIPTION
          "string literal value that
          could span multiple lines"

它将仅当文件以换行符结尾时才查找最后一个块/标识符

Try this, which should work in perl regular expressions:

([\w\d]*)\s+BLOCK\s*\n(\s*(NAME|DESCRIPTION|TYPE|...)\s*([\w\d]*|"(.*)")\s*\n)+

I verified it at REGex TESTER using the following test text:

blockName BLOCK
     NAME value
     NAME value
     DESCRIPTION
          "string literal value that
          could span multiple lines"
otherName BLOCK
     NAME value
     TYPE value
     DESCRIPTION
          "string literal value that
          could span multiple lines"

It will only find the last block/identifier if the file ends in a newline

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文