我现在花了三天时间编写正则表达式,以便在多行上匹配多字符串。 我的文件有如下文字:
[pid 20242] 23:13:36 futex(0x7f8087eb18, FUTEX_WAKE, 1 <unfinished ...>
[pid 621] 23:13:36 futex(0x7f80855410, FUTEX_WAIT, 1041, NULL <unfinished ...>
[pid 20242] 23:13:36 <... futex resumed> ) = 0
[pid 621] 23:13:36 <... futex resumed> ) = -1 EAGAIN (Try again)
[pid 20242] 23:13:36 munmap(0x7f80200000, 8192 <unfinished ...>
--> [pid 621] 23:13:36 openat(AT_FDCWD, "/proc/self/task", O_RDONLY|O_DIRECTORY|O_CLOEXEC <unfinished ...>
[pid 20242] 23:13:36 <... munmap resumed> ) = 0
[pid 621] 23:13:36 <... openat resumed> ) = 13
[pid 20242] 23:13:36 madvise(0x7f76a7b000, 20480, MADV_DONTNEED <unfinished ...>
--> [pid 621] 23:13:36 hammad(13, FUTEX_WAKE, 1, 24 )
[pid 20242] 23:13:36 madvise(0x7f76a7b000, 20480, MADV_DONTNEED <unfinished ...>
[pid 621] 23:13:36 <... futex resumed> ) = 0
[pid 20242] 23:13:36 futex(0x7f80855410, FUTEX_WAKE, 1 <unfinished ...>
[pid 621] 23:13:36 futex(0x7f8087eb18, FUTEX_WAKE, 1 <unfinished ...>
[pid 20242] 23:13:36 <... futex resumed> ) = 0
[pid 621] 23:13:36 <... futex resumed> ) = 0
[pid 20242] 23:13:36 futex(0x7f8087eb18, FUTEX_WAKE, 1 <unfinished ...>
[pid 621] 23:13:36 futex(0x7f80855410, FUTEX_WAIT, 1041, NULL <unfinished ...>
[pid 20242] 23:13:36 <... futex resumed> ) = 0
我添加了“ - &gt;”为了清楚上面我查找的字符串。但是,我需要找到是否存在“openat(AT_FDCWD,”/ proc / self / task“,O_RDONLY | O_DIRECTORY | O_CLOEXEC”后面是hammad(13,FUTEX_WAKE,1,24)的模式。它可以在那里ae它们之间存在多线,但重要的是“openat”,其次是“hammad”函数。
我有很多不同文本的文件,但我想使用相同的模式进行匹配。 这是我的代码:
text = open('textfile.txt').read()
if re.findall(r"[a-zA-Z\s.-]*openat(AT_FDCWD, "/proc/self/task",
O_RDONLY|O_DIRECTORY|O_CLOEXEC <unfinished ...>([a-zA-Z0-9|\s|.])*hammad(13, FUTEX_WAKE, 1, 24 )", text):
print 'found a match!'
else:
print 'no match'
有人可以帮我修改我的代码吗? 感谢
答案 0 :(得分:2)
您可以使用:
/^.*openat(?:[\s\S]*)^.*hammad.*/gm
关键要素是:
m
标志; .*
匹配任何水平字符但与换行符不匹配(除非您使用s
标志); [\s\S]*
匹配任何字符,包括换行符。Python演示:
>>> re.findall(r"^.*openat(?:[\s\S]*)^.*hammad.*", txt, re.M)
['--> [pid 621] 23:13:36 openat(AT_FDCWD, "/proc/self/task", O_RDONLY|O_DIRECTORY|O_CLOEXEC <unfinished ...>\n[pid 20242] 23:13:36 <... munmap resumed> ) = 0\n[pid 621] 23:13:36 <... openat resumed> ) = 13\n[pid 20242] 23:13:36 madvise(0x7f76a7b000, 20480, MADV_DONTNEED <unfinished ...>\n --> [pid 621] 23:13:36 hammad(13, FUTEX_WAKE, 1, 24 )']
如果您想在hammad
之后添加文字限定字符,请记住转义任何正则表达式元字符。
答案 1 :(得分:1)
最终,就正则表达而言,像@dawg这样的答案显然要简单得多。
话虽如此,作为一般的Python和Regex学习机会,您的示例中有一些事情突然导致错误。
@dawg通过编写一个没有这些问题的更简洁的正则表达式来隐含地解决所有这些问题,但我认为这是你发布的问题的根源。