我正在用python做正则表达式。我花了很多时间来修复问题,并知道为什么正则表达式不起作用或不匹配。我在此处发布文本和代码。我可以知道我的代码有什么问题吗:
文本为:
[pid 30101] 04:15:46 writev(25, [{"\0\225ub'\375[\340\244\6/", 11}, {"\4", 1},
{"Id before\0", 17}, {"Id\0", 10}], 4) = 39
[pid 30101] 04:15:46 getuid() = 10168
[pid 30101] 04:15:46 getuid() = 10168
[pid 30101] 04:15:46 ioctl(8, BINDER_WRITE_READ, 0x7fc0656648) = 0
[pid 30101] 04:15:46 ioctl(8, BINDER_WRITE_READ, 0x7fc0656648) = 0
[pid 30101] 04:15:46 fstat(33, {st_mode=S_IFCHR|0666, st_rdev=makedev(10, 62),
...}) = 0
[pid 30101] 04:15:46 fstat(33, {st_mode=S_IFCHR|0666, st_rdev=makedev(10, 62),
...}) = 0
[pid 30101] 04:15:46 ioctl(33, ASHMEM_GET_SIZE, 0) = 84
[pid 30101] 04:15:46 dup(33) = 34
[pid 30101] 04:15:46 close(33) = 0
[pid 30101] 04:15:46 dup(34) = 33
[pid 30101] 04:15:46 fstat(33, {st_mode=S_IFCHR|0666, st_rdev=makedev(10, 62),
...}) = 0
[pid 30101] 04:15:46 ioctl(33, ASHMEM_GET_SIZE, 0) = 84
[pid 30101] 04:15:46 mmap(NULL, 84, PROT_READ, MAP_SHARED, 33, 0) =
0x7d517db000
[pid 30101] 04:15:46 fstat(33, {st_mode=S_IFCHR|0666, st_rdev=makedev(10, 62),...}) = 0
[pid 30101] 04:15:46 ioctl(33, ASHMEM_GET_SIZE, 0) = 84
[pid 30101] 04:15:46 fstat(34, {st_mode=S_IFCHR|0666, st_rdev=makedev(10, 62),
...}) = 0
[pid 30101] 04:15:46 fstat(34, {st_mode=S_IFCHR|0666, st_rdev=makedev(10, 62),...}) = 0
[pid 30101] 04:15:46 ioctl(34, ASHMEM_GET_SIZE, 0) = 84
[pid 30101] 04:15:46 close(34) = 0
[pid 30101] 04:15:46 getuid() = 10168
[pid 30101] 04:15:46 writev(25, [{"\0\225ub'\375[\260y\3274", 11}, {"\4", 1},
{"After\0", 16}, {"eb41e1a15da0b8ee\0", 17}], 4) = 45
[pid 30101] 04:15:46 ioctl(8, BINDER_WRITE_READ, 0x7fc0656ee8) = 0
[pid 30101] 04:15:46 ioctl(8, BINDER_WRITE_READ, 0x7fc0656ee8) = 0
这是我的代码:
import re
text = open('textfile.txt').read()
pid= str(30101)
if re.findall(r"^.* " + pid +"] \d\d:\d\d:\d\d getuid()^.*" + pid +"]\d\d:\d\d:\d\d ioctl\(8\, BINDER_WRITE_READ\, 0x7fc0656648\)^.*" + pid +"] \d\d:\d\d:\d\d fstat\(33\, \{st_mode=S_IFCHR\|0666\, st_rdev=makedev\(10\, 62\)\,
...\}\).*", text, re.M):
print 'found a match!'
else:
print 'no match'
答案 0 :(得分:1)
在开发正则表达式时尝试从小处着手。某些字符(例如public interface IUser
{
int AthleteId { get; set; }
string GivenName { get; set; }
string FamilyName { get; set; }
bool IsActive { get; set; }
}
public class DjsNameAutoSearch<Result, User> : where User : class, IUser, new()
,[
,]
,(
等)具有特殊含义。如果要从字面上匹配这些字符,则需要通过在字符前面附加)
来对其进行转义。
使用转义符的正则表达式的简化版本:
\
产生以下输出:
import re
text = open('textfile.txt').read()
pid= str(30101)
print re.findall(r"^.* " + pid + "\] \d\d:\d\d:\d\d getuid\(\)", text, re.M)
我建议阅读re
模块的python文档,其中介绍了语法,然后逐步扩展正则表达式。
这能回答您的问题吗?
答案 1 :(得分:0)
感谢所有人,我已经尝试过regex101.com非常有帮助,并且很好用python构建了regex。我尝试创建正则表达式,但遇到了问题。在第10行和第12行有两个dup()匹配。每次,我的正则表达式都匹配第12行的dup(),但是我想找到下一个第一个匹配的10行。我想比赛是从上到下而不是从下到上。 由于[\ s \ S] +,我觉得有问题。 我可以正确使用吗?
这是我的尝试: https://regex101.com/r/5WpzKV/3