[英]Why my Python RegExp doesn't works wih files with carry returns?
I need read a file.我需要读一个文件。 This file contains a list with key-value.
该文件包含一个带有键值的列表。 The key is always the same ['key_text'] and the value is always a text or a html or a list... My regular expression works without "carry return".
键始终是相同的 ['key_text'] 并且值始终是文本或 html 或列表......我的正则表达式在没有“进位返回”的情况下工作。 But when exists \r\n or \n in "value", not works.
但是当“值”中存在 \r\n 或 \n 时,不起作用。
This is my regexp ( very bad I known ):这是我的正则表达式(我知道非常糟糕):
^[ ]*[ \t]*(\[\'([\w+\.]*)\'\])[ ]*[ \t]*\=[ ]*[ \t]*[\'\"](.*)[\'\"][ ]*[ \t]*\;
I have this file, for example:我有这个文件,例如:
['key1'] = 'value1';
['key2'] = 'value2';
['key3'] = 'value3';
['key4'] = '<p>text text text text
text text text text text text text text text
text text text </p><ul>text
text text text text text text
text</p>';
['key5'] = 'value5';
How can I read this text file and make "groups and matches" like this:如何阅读此文本文件并像这样进行“组和匹配”:
Match1: ['key1'] = 'value1';匹配1:['key1'] = 'value1';
Match2: ['key2'] = 'value2';匹配2:['key2'] = 'value2';
Match3: ['key3'] = '匹配 3:['key3'] = '
text text text text text text text text text text text text text text text text文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本
text text text text文字文字文字文字
text text text text text text text text text文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本
text text text文字文字文字
text文本
Match4: ['key4'] = 'value4';匹配4:['key4'] = 'value4';
str = """['key1'] = 'value1';
['key2'] = 'value2';
['key3'] = 'value3';
['key4'] = '<p>text text text text
text text text text text text text text text
text text text </p><ul>text
text text text text text text
text</p>';
['key5'] = 'value5';
"""
re.findall(r"\['(?P<key>\w+)'\]\s*=\s*'(?P<value>[^']+)",str,re.MULTILINE)
results结果
[('key1', 'value1'),
('key2', 'value2'),
('key3', 'value3'),
('key4', '<p>text text text text \n\n\ntext text text text text text text text text \n\n\n text text text </p><ul>text\n text text text text text text\n\n\n text</p>'),
('key5', 'value5')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.