searching substrings with interpositions
Kent Johnson
kent37 at tds.net
Tue May 24 05:55:18 EDT 2005
borges2003xx at yahoo.it wrote:
> hi everyone.
> a problem:
> two binary strings, a="0101" b="000011110100";
> i search a function f(a,b) that gives 1 if a is "contained" in b with
> any sub strings interposed.
> in this example a in contained cause 000<01>111<01>00 but also
> 0<0>001111<101>00"
> but also <0>0001111<101>00 but also 000<0>1111<01>0<0> etc....
> any idea?
> Thanx in advance.
> Giorgi Borghi
You can do this easily with regular expressions though I guess may be poor with long strings:
>>> import re
>>> re.search('0.*1.*0.*1', '000011110100')
<_sre.SRE_Match object at 0x008D9BF0>
>>> _.span()
(0, 10)
Put the chars of the search string in groups if you need to know where they were found.
Kent
More information about the Python-list
mailing list