Chen Chen - 1 year ago 53
Python Question

'(match_start1)...(match_start2)...(match_end)' find the shortest string match

I've read over this post http://stackoverflow.com/questions/2148700/how-do-i-find-the-shortest-overlapping-match-using-regular-expressions but the answer there doesn't work for my case.

I was looking for pattern

'\D\d{2,5}?.+?CA.??[ \-\.\_]*(?:\d{5})?',
the string is '6785 56767at435 hjfioej st. CA. 94827ifojwnf 93842'


Both
't435 hjfioej st. CA. 94827'
and
' 56767at435 hjfioej st. CA. 94827'
should match, but the procedure given in the above post is not giving the minimum match.

Please help.

Answer Source
re.findall('(?=(\D\d{2,5}?.+?CA.?[ -._]*(?:\d{5})?))','6785 56767at435 hjfioej st. CA. 94827ifojwnf 93842')

So this will do the trick, then output the one of the minimum length.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download