2

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.

In strings:

strings -n 3 mke2fs | grep -E '^([0-9][0-9]*(\.[0-9]+)+)'

the result: 1.41.11

In 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)? I tried using regex (another re alternative) still with no result.

4

1 に答える 1

6

grepのようにテキストを処理するには、re.MULTILINEフラグが必要です。^

ところで、使用する方が読みやすい\dです:

for c in re.finditer(r'^(\d+(\.\d+)+)', f, re.MULTILINE):
    print c.group(1)
于 2013-02-26T02:37:29.653 に答える