Regular Expressions
Tim Chase
python.list at tim.thechases.com
Wed Apr 12 15:47:35 EDT 2006
> I am trying to grab the following string out of a text file using regular
> expression (re module):
While I'm all for using regexps when they're needed, are you
sure you need the overhead of regexps?
f = open("foo.txt", "r")
for line in f.readlines():
# if '"DcaVer"=dword:' in line:
if line.startswith('"DcaVer"=dword:'):
value = int(line.split(":", 1)[-1], 16)
print value
f.close()
If you absolutely must use a regexp,
import re
f = open("foo", "r")
r = re.compile(r'"DcaVer"=dword:([0-9a-fA-F]{7})')
for line in f.readlines():
m = r.match(line)
if m:
value = int(m.group(1), 16)
print value
f.close()
should do the trick for you. This assumes that each string
of hex digits has seven characters. Adjust accordingly.
-tim
More information about the Python-list
mailing list