FAQ
I would like to get the all positions of a pattern match in a string.

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

output:
6
14
37
49
56
69
73
78

Karl

--
----------------------------------------------------------------------
Karl Schmid
Max-Planck-Institute for Chemical Ecology
Carl-Zeiss-Promenade 10 Tel: +49 3641 / 643658
07745 Jena Fax: +49 3641 / 643669
Germany Email: schmid at ice.mpg.de
======================================================================

Search Discussions

  • Fredrik Lundh at Apr 6, 2001 at 1:49 pm

    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
  • Alex Martelli at Apr 6, 2001 at 2:48 pm
    "Karl Schmid" <schmid at ice.mpg.de> wrote in message
    news:9akdum$2ve8$1 at gwdu67.gwdg.de...
    I would like to get the all positions of a pattern match in a string.
    Is there a more elegant solution to this problem than the following one?
    Depends on your definition of 'elegance', I guess, but...:

    class Matches:
    def __init__(self):
    self.matches = []
    def __call__(self, mo):
    self.matches.append(mo.start())
    return ''

    import re
    a = '''This module provides regular expression matching
    operations similar to those found in Perl'''

    p = re.compile('(o)')
    matches = Matches()
    junk = p.sub(matches, a)

    for match in matches.matches:
    print match



    Some would call it "elegant", some would call it "tricky"
    (because the .sub is being used only for its side effects,
    and its result is uninteresting and gets discarded).


    Anyway, the .sub method is the only documented way I know
    to get a *call-back* for every non-overlapping match, "at a
    single stroke", without explicitly programming a loop
    yourself. (The "non-overlapping" feature may not be
    what you're looking for, of course, in which case this
    approach is certainly ungood).


    If I did decide I had better program a loop (e.g., I also
    want matches that would overlap with others), and I'm not
    being paranoic about performance (if I needed to be, I
    would carefully time each possible alternative), I think
    I'd code it the simplest way I can think of...:

    for i in range(len(a)):
    if p.match(a[i:]):
    print i


    Alex

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 6, '01 at 12:47p
activeApr 6, '01 at 2:48p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase