Iteration within re.sub()?

Discussion in 'Python' started by Bryant Huang, Dec 14, 2004.

  1. Bryant Huang

    Bryant Huang Guest

    Hi,

    Is it possible to perform iteration within the re.sub() function call?

    For example, if I have a string like:

    str = "abbababbabbaaa"

    and I want to replace all b's with an integer that increments from 0,
    could I do that with re.sub()?

    Replacing b's with 0's is trivial:

    i = 0
    pat = re.compile("b")
    print pat.sub(`i`, str)

    Now, how can I increment i in each replacement? Is this possible? Like,
    using a lambda function, for example? Or do I use a different re
    function altogether?

    I use this trivial [ab]* language for simplicity, but I have a real
    problem where I need to match a more complex regex and replace it with
    an incrementing integer.

    Thanks so much!
    Bryant
    Bryant Huang, Dec 14, 2004
    #1
    1. Advertising

  2. Bryant Huang wrote:

    >Hi,
    >
    >Is it possible to perform iteration within the re.sub() function call?
    >
    >

    Sure. As the docs note:

    If repl is a function, it is called for every non-overlapping occurrence
    of pattern. The function takes a single match object argument, and
    returns the replacement string. For example:

    #!/usr/bin/env python

    import re

    class Counter:
    def __init__(self):
    self.count = -1
    def increment(self, matchObject):
    self.count += 1
    return str(self.count)

    text = "abbababbaaaaaabbaaa"
    expected = "a01a2a34aaaaaa56aaa"

    # Replace all b's with an integer that increments from 0.

    c = Counter()
    pat = re.compile("(b)")
    actual = pat.sub(c.increment, text)
    assert expected == actual
    Mark McEahern, Dec 14, 2004
    #2
    1. Advertising

  3. Bryant Huang

    Bryant Huang Guest

    Ah beautiful, thank you both, Robert and Mark, for your instant and
    helpful responses. I understand, so the basic idea is to keep a
    variable that is globally accessible and call an external function to
    increment that variable...

    Thanks!
    Bryant
    Bryant Huang, Dec 14, 2004
    #3
  4. Bryant Huang wrote:

    > Ah beautiful, thank you both, Robert and Mark, for your instant and
    > helpful responses. I understand, so the basic idea is to keep a
    > variable that is globally accessible and call an external function to
    > increment that variable...


    accessible for the callback function, that is.

    see Mark's example for the right way to do it (using a object to hold
    the state, and an object method as the callback -- the latter is known
    as a "bound method", and holds a reference to both the data (self) and
    the code).

    </F>
    Fredrik Lundh, Dec 14, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wijaya Edward
    Replies:
    1
    Views:
    230
    Marc 'BlackJack' Rintsch
    Oct 26, 2006
  2. Ben
    Replies:
    2
    Views:
    853
  3. Rudi
    Replies:
    5
    Views:
    4,941
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    1,937
    Lawrence D'Oliveiro
    May 20, 2011
  5. wpy

    how to create sub control within sub control?

    wpy, Apr 7, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    136
    Stefan
    Apr 8, 2004
Loading...

Share This Page