[英]Why doesn't this regular expression work on python
I am trying to extract some string from a binary file. 我正在尝试从二进制文件中提取一些字符串。 When I use this regular expression with strings in linux it works fine but it does not work in python.
当我在Linux中将此正则表达式与字符串一起使用时,它可以正常工作,但在python中不起作用。
In strings : 在字符串中 :
strings -n 3 mke2fs | grep -E '^([0-9][0-9]*(\.[0-9]+)+)'
the result: 1.41.11 结果: 1.41.11
In python : 在python中 :
import re
f = open("mke2fs","rb").read()
for c in re.finditer('^([0-9][0-9]*(\.[0-9]+)+)',f):
print c.group(1)
The result is empty. 结果为空。 How can I resolve this?
我该如何解决? Is it because of my Python version (I'm using Python 2.7)?
是因为我的Python版本(我正在使用Python 2.7)吗? I tried using regex (another re alternative) still with no result.
我仍然尝试使用正则表达式(另一种替代方法),但没有结果。
You need the re.MULTILINE
flag for ^
to work on your text like grep do. 您需要
^
的re.MULTILINE
标志才能像grep一样处理文本。
BTW, it is more readable to use \\d
: 顺便说一句,使用
\\d
更具可读性:
for c in re.finditer(r'^(\d+(\.\d+)+)', f, re.MULTILINE):
print c.group(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.