简体   繁体   English

Python regex搜索/ findall。 对于配置格式

[英]Python regex search/findall. For a config format

I have a config file in the following format 我有以下格式的配置文件

[COMPONENT1]
KEY1=Value
KEY2=Value
KEY3=Value

[COMPONENT2]
KEY1=Value
KEY2=Value
KEY3=Value
KEY4=Value

I am having trouble writing single line regex for re.findall() , where i can get list/tuple of "COMPONENT"(s) and its respective "KEY(n)" - "VALUE" pairs to iterate through. 我在为re.findall()写单行正则表达式时遇到麻烦,在这里我可以获取“ COMPONENT”及其相应的“ KEY(n)”-“ VALUE”对的列表/元组以进行迭代。

so far i have tried the following regex 到目前为止,我已经尝试了以下正则表达式

with open(conf,"r") as config:    
    match = re.findall(r,"?:\[(\w+)\](?:\s*\n*)(?:(\w+(?:\s*=\s*).+)))", config.read())

It's returning 回来了

Match 1
Group1: 'COMPONENT1'
Group2: 'KEY1=VALUE'

Match 2
Group1: 'COMPONENT2'
Group2: 'KEY1=VALUE'

I am unable to formulate a regex that can show other 'Key=Value' pair. 我无法制定可以显示其他“键=值”对的正则表达式。

Any help on this is really appreciated. 对此,我们将给予任何帮助。

Note: This config format cannot be changed. 注意:此配置格式不能更改。

This is not something I would suggest using regular expressions for. 我不建议使用正则表达式。 Regular expressions can be great, but when trying to work with something like a configuration file, they aren't very helpful in structuring what you want to read. 正则表达式可能很棒,但是当尝试使用配置文件之类的东西时,它们对构造您想要读取的内容并不是很有帮助。 Unless you can guarantee that every single relevant line will be structured similarly to [Section Name] or key=value , and only take up one line, and yadda yadda yadda, a regular expression will only complicate the parsing and use of a config file. 除非您可以保证每个相关行的结构都类似于[Section Name]key=value并且仅占用一行, yadda yadda yadda,则正则表达式只会使配置文件的解析和使用变得复杂。

In the regular expression you shared, you will only match if and only if a [Section Name] is followed by a key=value pair. 在您共享的正则表达式中,只有当[Section Name]后跟key=value对时,您才匹配。 key=value pairs on their own lines are being ignored because they are not preceded by a [Section Name] . key=value自行上的key=value对将被忽略,因为它们之前没有[Section Name] This is part of what makes using regular expressions a bit inappropriate for things like reading configurations; 这就是使得使用正则表达式对读取配置等内容不适当的部分原因。 there are conditionals that arise when structuring your config file that must be dealt with in your regular expression, making it longer and more complicated than it needs to be for the task, or should be when someone needs to maintain it. 在构造您的配置文件时会出现一些条件,必须在您的正则表达式中处理它,从而使其变得比任务所需的时间更长或更复杂,或者需要有人维护它时。

ConfigParser is a python module that allows you to easily read .ini-style config files, and I would suggest trying to use it as most of the hard work in reading arbitrary key=value pairs is already solved. ConfigParser是一个python模块,可让您轻松读取.ini样式的配置文件,我建议尝试使用它,因为读取任意key=value对的大部分艰苦工作已经解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM