Re: Find first matching substring

2009-07-17 Thread John Machin
On Jul 18, 2:11 am, Eloff wrote: > Almost every time I've had to do parsing of text over the last 5 years > I've needed this function: [snip] > It finds the first matching substring in the target string, if there > is more than one match, it returns the position of the match that > occurs at the l

Re: Find first matching substring

2009-07-17 Thread MRAB
Eloff wrote: > Almost every time I've had to do parsing of text over the last 5 years > I've needed this function: > > def find_first(s, subs, start=None, end=None): > results = [s.find(sub, start, end) for sub in subs] > results = [r for r in results if r != -1] > if results: >