[Tutor] regular expression question

Kelie kf9150 at gmail.com
Tue Apr 28 10:03:00 CEST 2009


Hello,

The following code returns 'abc123abc45abc789jk'. How do I revise the pattern so
that the return value will be 'abc789jk'? In other words, I want to find the
pattern 'abc' that is closest to 'jk'. Here the string '123', '45' and '789' are
just examples. They are actually quite different in the string that I'm working
with. 

import re
s = 'abc123abc45abc789jk'
p = r'abc.+jk'
lst = re.findall(p, s)
print lst[0]

Thanks for your help!



More information about the Tutor mailing list