python正则表达式不起作用[重复]

时间:2021-01-12 21:45:28

This question already has an answer here:

这个问题在这里已有答案:

I have this android logcat's log:

我有这个android logcat的日志:

"Could not find class android.app.Notification$Action$Builder, referenced from method b.a"

“无法找到类android.app.Notification $ Action $ Builder,从方法b.a引用”

and I'm trying to apply a regular expression, in python, to extract android.app.Notification$Action$Builder and b.a.

我正在尝试在python中应用正则表达式来提取android.app.Notification $ Action $ Builder和b.a.

I use this code:

我用这个代码:

regexp = '\'([\w\d\.\$\:\-\[\]\<\>]+).*\s([\w\d\.\$\:\-\[\]\<\>]+)'
match = re.match(r'%s' % regexp, msg, re.M | re.I)

I tested the regular expression online and it works as expected, but it never matches in python. Someone can give me some suggestions?

我在线测试了正则表达式,它按预期工作,但它在python中永远不会匹配。有人可以给我一些建议吗?

Thanks

谢谢

1 个解决方案

#1


1  

.re.match() matches only at the start of a string. Use re.search() instead, see match() vs. search().

.re.match()仅匹配字符串的开头。请改用re.search(),参见match()和search()。

Note that you appear to misunderstand what a raw string literal is; r'%s' % string does not produce a special, different object. r'..' is just notation, it still produces a regular string object. Put the r on the original string literal instead (but if you use double quotes you do not need to quote the single quote contained):

请注意,您似乎误解了原始字符串文字是什么; r'%s'%string不会产生特殊的不同对象。 r'..'只是表示法,它仍然产生一个常规的字符串对象。改为将r放在原始字符串文字上(但如果使用双引号,则不需要引用包含的单引号):

regexp = r"'([\w\d\.\$\:\-\[\]\<\>]+).*\s([\w\d\.\$\:\-\[\]\<\>]+)"

For this specific regex it doesn't otherwise matter to the pattern produced.

对于这个特定的正则表达式,它对所产生的模式无关紧要。

Note that the pattern doesn't actually capture what you want to capture. Apart from the escaped ' at the start (which doesn't appear in your text at all, it won't work as it doesn't require dots and dollars to be part of the name. As such, you capture Could and b.a instead, the first and last words in the regular expression.

请注意,该模式实际上并不捕获您要捕获的内容。除了在开始时逃脱(根本没有出现在你的文本中,它不会起作用,因为它不需要点和美元作为名称的一部分。因此,你捕获了Can和ba ,正则表达式中的第一个和最后一个单词。

I'd anchor on the words class and method instead, and perhaps require there to be dots in the class name:

我会在类和方法上锚定,也许需要在类名中加点:

regexp = r'class\s+((?:[\w\d\$\:\-\[\]\<\>]+\.)+[\w\d\$\:\-\[\]\<\>]+).*method ([\w\d.\$\:\-\[\]\<\>]+)'

Demo:

演示:

>>> import re
>>> regexp = r'class\s+((?:[\w\d\$\:\-\[\]\<\>]+\.)+[\w\d\$\:\-\[\]\<\>]+).*method ([\w\d.\$\:\-\[\]\<\>]+)'
>>> msg = "Could not find class android.app.Notification$Action$Builder, referenced from method b.a"
>>> re.search(regexp, msg, re.M | re.I)
<_sre.SRE_Match object at 0x1023072d8>
>>> re.search(regexp, msg, re.M | re.I).groups()
('android.app.Notification$Action$Builder', 'b.a')

#1


1  

.re.match() matches only at the start of a string. Use re.search() instead, see match() vs. search().

.re.match()仅匹配字符串的开头。请改用re.search(),参见match()和search()。

Note that you appear to misunderstand what a raw string literal is; r'%s' % string does not produce a special, different object. r'..' is just notation, it still produces a regular string object. Put the r on the original string literal instead (but if you use double quotes you do not need to quote the single quote contained):

请注意,您似乎误解了原始字符串文字是什么; r'%s'%string不会产生特殊的不同对象。 r'..'只是表示法,它仍然产生一个常规的字符串对象。改为将r放在原始字符串文字上(但如果使用双引号,则不需要引用包含的单引号):

regexp = r"'([\w\d\.\$\:\-\[\]\<\>]+).*\s([\w\d\.\$\:\-\[\]\<\>]+)"

For this specific regex it doesn't otherwise matter to the pattern produced.

对于这个特定的正则表达式,它对所产生的模式无关紧要。

Note that the pattern doesn't actually capture what you want to capture. Apart from the escaped ' at the start (which doesn't appear in your text at all, it won't work as it doesn't require dots and dollars to be part of the name. As such, you capture Could and b.a instead, the first and last words in the regular expression.

请注意,该模式实际上并不捕获您要捕获的内容。除了在开始时逃脱(根本没有出现在你的文本中,它不会起作用,因为它不需要点和美元作为名称的一部分。因此,你捕获了Can和ba ,正则表达式中的第一个和最后一个单词。

I'd anchor on the words class and method instead, and perhaps require there to be dots in the class name:

我会在类和方法上锚定,也许需要在类名中加点:

regexp = r'class\s+((?:[\w\d\$\:\-\[\]\<\>]+\.)+[\w\d\$\:\-\[\]\<\>]+).*method ([\w\d.\$\:\-\[\]\<\>]+)'

Demo:

演示:

>>> import re
>>> regexp = r'class\s+((?:[\w\d\$\:\-\[\]\<\>]+\.)+[\w\d\$\:\-\[\]\<\>]+).*method ([\w\d.\$\:\-\[\]\<\>]+)'
>>> msg = "Could not find class android.app.Notification$Action$Builder, referenced from method b.a"
>>> re.search(regexp, msg, re.M | re.I)
<_sre.SRE_Match object at 0x1023072d8>
>>> re.search(regexp, msg, re.M | re.I).groups()
('android.app.Notification$Action$Builder', 'b.a')