A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2001-April/111952.html below:

How to get positions of multiple RE matches?

How to get positions of multiple RE matches? How to get positions of multiple RE matches?Fredrik Lundh fredrik at pythonware.com
Fri Apr 6 09:49:25 EDT 2001
Karl Schmid wrote:
> Is there a more elegant solution to this problem than the following one?
>
> >>> import re
> >>> a = 'This module provides regular expression matching operations
> similar to those found in Perl'
> >>> import re
> >>> p = re.compile('(o)')
> >>> pos = 0
> >>> while p.search(a, pos):
> ...    pos = p.search(a, pos).start()
> ...    print pos
> ...    pos += 1

better make that loop:

while 1:
    m = p.search(a, pos)
    if not m:
        break
    pos = m.start()
    print pos
    pos += 1

Cheers /F



More information about the Python-list mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4