Python RegEx匹配每个括号元素

时间:2017-05-08 17:49:42

标签: python regex

我一直试图为以下字符串提出一个正则表达式:

[1,null,"7. Mai 2017"],[2,"test","8. Mai 2018"],[3,"test","9. Mai 2019"]

我试图将每个括号的匹配输出与其内容作为单个元素进行匹配,如下所示:

[1,null,"7. Mai 2017"]
[2,"test","8. Mai 2018"]
[3,"test","9. Mai 2019"]

我最初的天真方法是这样的:

(\[[^d],.+\])+

但是,。+规则过于笼统,最终会匹配整行。 任何提示?

3 个答案:

答案 0 :(得分:1)

以下代码将使用\[[^]]*]输出您要求的内容。

import re
regex = r'\[[^]]*]'
line = '[1,null,"7. Mai 2017"],[2,"test","8. Mai 2018"],[3,"test","9. Mai 2019"]'
row = re.findall(regex, line)
print(row)

输出:

  

[' [1,NULL," 7。 Mai 2017"]',' [2," test"," 8。 Mai 2018"]',' [3," test"," 9。 Mai 2019"]']

考虑将null更改为None,因为它与python表示匹配。

答案 1 :(得分:1)

我不确定您要解析的数据格式及其来源,但它看起来像JSON。对于此特定字符串,从字符串的开头和结尾添加方括号使其 JSON可加载

In [1]: data = '[1,null,"7. Mai 2017"],[2,"test","8. Mai 2018"],[3,"test","9. Mai 2019"]'

In [2]: import json

In [3]: json.loads("[" + data + "]")
Out[3]: 
[[1, None, u'7. Mai 2017'],
 [2, u'test', u'8. Mai 2018'],
 [3, u'test', u'9. Mai 2019']]

请注意null如何成为Python的None

答案 2 :(得分:1)

您可以考虑使用精彩模块pyparsing来执行此操作:

import pyparsing 

for match in pyparsing.originalTextFor(pyparsing.nestedExpr('[',']')).searchString(exp):
    print match[0]
[1,null,"7. Mai 2017"]
[2,"test","8. Mai 2018"]
[3,"test","9. Mai 2019"]

(除非它实际上是JSON - 如果是这样的话,请使用JSON模块......)