Find first matching substring

MRAB python at mrabarnett.plus.com
Fri Jul 17 13:59:09 EDT 2009


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:
 >         return min(results)
 >
 >     return -1
 >
 > 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 lowest index in the string.
 >
One possible optimisation for your code is to note that if you find that
one of the substrings starts at a certain position then you're not
interested in any subsequent substring which might start at or after
that position, so you could reduce the search space for each substring
found.

> Has anyone else had problems where they could have applied this 
> function?
> 
> It seems to me that python's find (and rfind, index, rindex) could be
>  modified (the same way that startswith and endswith have been) to 
> behave this way if a tuple were passed. Do other's agree that this 
> would be desirable?
> 
Possibly. I think that allowing a tuple in the partition and rpartition
methods might also be useful.




More information about the Python-list mailing list