[英]re.search Multiple lines Python
re.search with \\s or '\\n' is not finding the multiline i'm trying to search for. 使用\\ s或'\\ n'进行re.search找不到我正在尝试搜索的多行。
Portion of Source: 来源部分:
Date/Time:
2013-08-27 17:05:36
----- BEGIN SEARCH -----
GENERAL DATA:
NAME: AB12
SECTOR:
999,999
CONTROLLED BY: Player
ALLIANCE: Aliance
ONLINE: 1 seconds ago
SIZE: Large
HOMEWORLD: NO
APPROVAL RATING: 100%
PRODUCTION RATE: 100%
RESOURCE DATA:
POWER: 0 / 0
BUILDINGS: 0 / 20
ORE: 80,000 / 80,000
CRYSTAL: 80,000 / 80,000
POPULATION: 40,000 / 40,000
BUILDING DATA:
N/A
UNIT DATA:
WYVERN(S): 100
----- END SEARCH -----
Looking at it in Notepad++ I see "BUILDING DATA:(LF)" 在Notepad ++中查看它我看到“BUILDING DATA:(LF)”
Full Code 完整代码
lines = open('scan.txt','r').readlines()
for a in lines:
if re.search(r"\A\d", a):
digits = a
if re.search(r"2013", digits):
date.append(digits[:19])
count +=1
elif re.search(r",", digits):
clean = digits.rstrip()
sector = clean.split(',')
x.append(sector[0])
y.append(sector[1])
elif re.search(r"CONTROLLED BY:", a):
player.append(a[15:].rstrip())
elif re.search(r"ALLIANCE:", a):
alliance.append(a[10:].rstrip())
elif re.search(r"SIZE:", a):
size.append(a[6:].rstrip())
elif re.findall('BUILDING DATA:\sN/A', a, re.M):
def_grid = ''
print "Didn't find it"
defense.append(def_grid)
defense_count +=1
elif re.search(r"DEFENSE GRID", a):
def_grid = a[16:].rstrip()
print "defense found"
defense_count +=1
But I am not having anything returned. 但我没有任何回报。
I need to put an empty spacer in when "DEFENSE GRID" doesn't exist after "BUILDING DATA:" 在“建立数据:”之后,当“DEFENSE GRID”不存在时,我需要放入一个空的垫片
I know i'm missing something and I've tried reading up on re.search but i'm not able to find any thorough examples that explain how the multiline works. 我知道我错过了一些东西而且我已经尝试阅读re.search但我无法找到解释多线如何工作的任何详尽的例子。
re.findall("BUILDING DATA:\nN/A",a,re.MULTILINE)
You can do just what you did, but using re.findall
instead of re.search
: 你可以做你做的,但使用
re.findall
而不是re.search
:
re.findall('BUILDING DATA:\nN/A', a, re.M)
#['BUILDING DATA:\nN/A']
EDIT: 编辑:
The problem is that you are currently reading line-by-line. 问题是你目前正在逐行阅读。 In order to detect a pattern that belongs to two or more lines, you have to consider the string as a whole, maybe doing:
为了检测属于两行或更多行的模式,您必须将字符串视为一个整体,可能会:
s = ''.join(lines)
which is ok if lines
is not so big, and then use s
to perform your multi-line searches... 如果
lines
不是那么大就没关系,然后用s
来执行你的多行搜索......
I wonder why you have nothing returned. 我想知道你为什么没有回来。 If your file looks like this:
如果您的文件如下所示:
BUILDING DATA:
N/A
I get using 我开始使用了
import re
f = open('test.txt','r')
a = f.read(20)
re.search('BUILDING DATA:\nN/A', a, re.M)
an output. 一个输出。 This is
这是
<_sre.SRE_Match object at 0x1004fc8b8>
If I test re.search with string, that is not in the file like in this code: 如果我用字符串测试re.search,那就不像在这段代码中那样在文件中:
import re
f = open('test.txt','r')
a = f.read(20)
re.search('BUILDING BATA:\nN/A', a, re.M)
there is no output as expected. 没有预期的输出。
EDIT: 编辑:
As Saullo Castro pointed out, the problem is the line-by-line reading. 正如Saullo Castro指出的那样,问题在于逐行阅读。 Why not use something like this?
为什么不使用这样的东西?
a = open('scan.txt','r').read()
if re.findall('BUILDING DATA:\nN/A', a, re.M):
print('found!')
3rd try: 第3次尝试:
tmp = False
...
elif re.findall('BUILDING DATA:', a, re.M):
tmp = True
elif tmp and re.findall('N/A', a, re.M):
def_grid = ''
print "Didn't find it"
defense.append(def_grid)
defense_count +=1
Replace 更换
re.findall('BUILDING DATA:\sN/A', a, re.M):
with 同
re.findall('BUILDING DATA:\nN/A', a, re.M):
or 要么
re.search(r'BUILDING DATA:\nN/A', a, re.M):
and it should work. 它应该工作。
(Notice that in your code there's \\s
instead of \\n
) (请注意,在您的代码中有
\\s
而不是\\n
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.