Python 正则表达式:如何处理行
Python RegEx: How to deal with lines
我有一个巨大的 txt 文件,其格式如下:
BadLine
property1=a
property2=b
BadLine2
property1=c
property2=d
GOODLINE1
property1=e
property2=f
...以及更多好的和坏的台词。
我需要做的是提取好线的属性(上例中的e和f)。
我可以很容易地在我的文件中找到好的行,但是我如何 select 只在与好的行相关联的块中搜索其他正则表达式的属性?
谢谢大家!
以下代码:
import re
test = '''
BadLine
property1=a
property2=b
BadLine2
property1=c
property2=d
GOODLINE1
property1=e
property2=f
BadLine
property1=a
property2=b
BadLine2
property1=c
property2=d
GOODLINE2
property1=e
property2=f
'''
pattern = r'^(GOODLINE(?:[^\n]|\n )*)'
print re.compile(pattern, re.MULTILINE).findall(test)
产生这些结果:
['GOODLINE1\n property1=e\n property2=f', 'GOODLINE2\n property1=e\n property2=f']
该模式匹配出现在行首的 "GOODLINE",以及其后非换行符的贪婪匹配字符,以及后跟 space 个字符的换行符。如果您的文本实际上在换行后有制表符而不是 spaces,您可以将 space 更改为制表符。或者,您可以通过像这样更改模式来轻松匹配:
pattern = r'^(GOODLINE(?:[^\n]|\n[ \t])*)'
一旦有了这些匹配项,就可以非常容易地使用常规字符串 split()
来提取属性。
或者,您可以查看 rson 包解析是否满足您的需求——这看起来是一个可以轻松解析的文件。
简短的回答是你可以:
GOODLINE[\d+]*\n.*property1=(.+)*\n.*property2=(.+)*\n?
在这种情况下,两个括号将是您要查找的值。如果您在以 windows/mac 样式创建的文件中有字符串,您将有不同的结束字符:windows 中的'\r\n' 和 mac 中的'\r' .在 linux 系统中,您将只有 '\n'。上面的模式将与字符串开头或结尾的任何 Goodline 匹配,即使末尾没有任何换行符。您在属性中的值也可以超过一个字符。
你可以试试一个非常有用的网站,Pythex来试试你的正则表达式。
您可以尝试的代码是:
import re
pattern = re.compile('GOODLINE[\d+]*\n.*property1=(.+)*\n.*property2=(.+)*\n?')
matchRes = re.findall(pattern,'''BadLine2
property1=c
property2=d
GOODLINE11
property1=e
property2=f
BadLine2
property1=c
property2=d
GOODLINE11
property1=eee34
property2=f00
BadLine2
property1=c
property2=d
GOODLINE1
property1=e
property2=f''');
if matchRes:
print matchRes
else:
print 'No match'
您将在列表中得到以下结果,每对是 property1 和 property2 值:
[('e', 'f'), ('eee34', 'f00'), ('e', 'f')]
我有一个巨大的 txt 文件,其格式如下:
BadLine
property1=a
property2=b
BadLine2
property1=c
property2=d
GOODLINE1
property1=e
property2=f
...以及更多好的和坏的台词。
我需要做的是提取好线的属性(上例中的e和f)。
我可以很容易地在我的文件中找到好的行,但是我如何 select 只在与好的行相关联的块中搜索其他正则表达式的属性?
谢谢大家!
以下代码:
import re
test = '''
BadLine
property1=a
property2=b
BadLine2
property1=c
property2=d
GOODLINE1
property1=e
property2=f
BadLine
property1=a
property2=b
BadLine2
property1=c
property2=d
GOODLINE2
property1=e
property2=f
'''
pattern = r'^(GOODLINE(?:[^\n]|\n )*)'
print re.compile(pattern, re.MULTILINE).findall(test)
产生这些结果:
['GOODLINE1\n property1=e\n property2=f', 'GOODLINE2\n property1=e\n property2=f']
该模式匹配出现在行首的 "GOODLINE",以及其后非换行符的贪婪匹配字符,以及后跟 space 个字符的换行符。如果您的文本实际上在换行后有制表符而不是 spaces,您可以将 space 更改为制表符。或者,您可以通过像这样更改模式来轻松匹配:
pattern = r'^(GOODLINE(?:[^\n]|\n[ \t])*)'
一旦有了这些匹配项,就可以非常容易地使用常规字符串 split()
来提取属性。
或者,您可以查看 rson 包解析是否满足您的需求——这看起来是一个可以轻松解析的文件。
简短的回答是你可以:
GOODLINE[\d+]*\n.*property1=(.+)*\n.*property2=(.+)*\n?
在这种情况下,两个括号将是您要查找的值。如果您在以 windows/mac 样式创建的文件中有字符串,您将有不同的结束字符:windows 中的'\r\n' 和 mac 中的'\r' .在 linux 系统中,您将只有 '\n'。上面的模式将与字符串开头或结尾的任何 Goodline 匹配,即使末尾没有任何换行符。您在属性中的值也可以超过一个字符。
你可以试试一个非常有用的网站,Pythex来试试你的正则表达式。
您可以尝试的代码是:
import re
pattern = re.compile('GOODLINE[\d+]*\n.*property1=(.+)*\n.*property2=(.+)*\n?')
matchRes = re.findall(pattern,'''BadLine2
property1=c
property2=d
GOODLINE11
property1=e
property2=f
BadLine2
property1=c
property2=d
GOODLINE11
property1=eee34
property2=f00
BadLine2
property1=c
property2=d
GOODLINE1
property1=e
property2=f''');
if matchRes:
print matchRes
else:
print 'No match'
您将在列表中得到以下结果,每对是 property1 和 property2 值:
[('e', 'f'), ('eee34', 'f00'), ('e', 'f')]