我花了两天时间尝试构建正则表达式,以发现两个单词/数字在不同的行上按顺序出现。 我有一个文件,其中包含如下文本:
1 [pid 29743] 18:58:19 prctl(PR_CAPBSET_DROP, 0x9, 0, 0, 0 <unfinished ...>
2 [pid 29746] 18:58:19 <... mprotect resumed> ) = 0
3 [pid 29743] 18:58:19 <... prctl resumed> ) = 0
4 [pid 615] 18:58:19 <... ioctl resumed> , 0xffffffffffb4f054) = 0
5 [pid 29743] 18:58:19 prctl(PR_CAPBSET_READ, 0xa, 0, 0, 0 <unfinished ...>
6 [pid 615] 18:58:19 ioctl(13, 0x40047703 <unfinished ...>
7 [pid 29743] 18:58:19 <... prctl resumed> ) = 1
8 [pid 29746] 18:58:19 mprotect(0xfffffffff4ae2000, 4096, PROT_NONE <unfinished ...>
9 [pid 29743] 18:58:19 prctl(PR_CAPBSET_DROP, 0xa, 0, 0, 0 <unfinished ...>
10 [pid 615] 18:58:19 <... ioctl resumed> , 0x7fd19062e0) = 0
11 [pid 29743] 18:58:19 <... prctl resumed> ) = 0
12 [pid 29746] 18:58:19 <... mprotect resumed> ) = 0
13 [pid 29743] 18:58:19 prctl(PR_CAPBSET_READ, 0xb, 0, 0, 0 <unfinished ...>
14 [pid 29746] 18:58:19 ioctl(13, 0x40047703,
<unfinished ...>
15 [pid 29743] 18:58:19 <... prctl resumed> ) = 1
16 [pid 615] 18:58:19 <... ioctl resumed> , 0x7fd19064b0) = 0
我正在寻找两个值0x7fd19062e0和0x7fd19064b0,这两个值在文本中顺序出现。它们出现在第10行和第16行。 我想构建一个正则表达式,告诉我是否按顺序出现 这是我的密码
file = open("text.txt", a+)
for line in file:
text += line
if re.findall(r"^.*0x7fd19062e0.*0x7fd19064b0", text, re.M):
print 'found a match!'
else:
print 'no match'
re.M
修改^
和$
锚的行为。对于“dot matches newline”选项,需要re.S
。另外,如果您只想查找是否存在匹配项,请不要使用re.findall()
:请注意,我添加了word boundary anchors以确保只匹配整个十六进制数(否则,可能会有更长数字的子匹配)。这可能与您的情况有关,也可能与您的情况无关,但这可能是一种很好的做法
无需重新:
相关问题 更多 >
编程相关推荐