passing multiple strings to string.find()
Bengt Richter
bokr at oz.net
Fri Aug 8 00:27:31 EDT 2003
On Thu, 07 Aug 2003 23:35:55 -0400, hokiegal99 <hokiegal99 at vt.edu> wrote:
>How do I say:
>
>x = string.find(files, 'this', 'that', 'the-other')
>
>currently I have to write it like this to make it work:
>
>x = string.find(files, 'this')
>y = string.find(files, 'that')
>z = string.find(files, 'the-other')
>
You might try the re module, e.g.,
>>> import re
>>> rxo = re.compile(r'this|that|the-other')
>>> pos = 0
>>> while 1:
... m = rxo.search(' Find this or the-other or that and this.', pos)
... if not m: break
... print '%4s: %s' % (m.start(), m.group())
... pos = m.end()
...
6: this
14: the-other
27: that
36: this
If some search strings have a common prefix, you'll have to put
the longest first in the regex, since re grabs the first match it sees.
Regards,
Bengt Richter
More information about the Python-list
mailing list