Trouble with regex

Discussion in 'Python' started by Fernando Rodriguez, Nov 14, 2003.

  1. Hi,

    I'm trying to write a regex that finds whatever is between ${ and } in a text
    file.

    I tried the following, but it only finds the first occurrence of the pattern:

    >>> s = """asssdf${123}

    dgww${one} ${two}"""
    >>> what = re.compile("\$\{([^}]*)\}")
    >>> m = what.search(s)
    >>> m.groups()

    ('123',)

    What am I doing wrong? O:)
    Fernando Rodriguez, Nov 14, 2003
    #1
    1. Advertising

  2. Fernando Rodriguez

    anton muhin Guest

    Fernando Rodriguez wrote:
    > Hi,
    >
    > I'm trying to write a regex that finds whatever is between ${ and } in a text
    > file.
    >
    > I tried the following, but it only finds the first occurrence of the pattern:
    >
    >
    >>>>s = """asssdf${123}

    >
    > dgww${one} ${two}"""
    >
    >>>>what = re.compile("\$\{([^}]*)\}")
    >>>>m = what.search(s)
    >>>>m.groups()

    >
    > ('123',)
    >
    > What am I doing wrong? O:)


    Nothing ;) search just finds first match. If you want to find all
    non-overlapping matches try finditer:

    import re

    s = """asssdf${123}

    dgww${one} ${two}"""

    what = re.compile("\$\{([^}]*)\}")
    for m in what.finditer(s):
    print m.groups()

    prints:
    ('123',)
    ('one',)
    ('two',)

    regards,
    anton.
    anton muhin, Nov 14, 2003
    #2
    1. Advertising

  3. Fernando Rodriguez

    Jim Shapiro Guest

    "Fernando Rodriguez" <> wrote in message
    news:...
    > Hi,
    >
    > I'm trying to write a regex that finds whatever is between ${ and } in a

    text
    > file.
    >
    > I tried the following, but it only finds the first occurrence of the

    pattern:
    >
    > >>> s = """asssdf${123}

    > dgww${one} ${two}"""
    > >>> what = re.compile("\$\{([^}]*)\}")
    > >>> m = what.search(s)
    > >>> m.groups()

    > ('123',)
    >
    > What am I doing wrong? O:)


    Try:

    import re

    s = """asssdf${123}
    dgww${one} ${two}"""
    what = re.compile("\$\{([^}]*)\}") # same as original
    m = what.findall(s)
    print m

    >regex_test.py

    ['123', 'one', 'two']

    HTH

    Jim Shapiro
    Jim Shapiro, Nov 14, 2003
    #3
    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. Hypo
    Replies:
    6
    Views:
    406
  2. Fernando Rodriguez
    Replies:
    5
    Views:
    397
    Roel Mathys
    Nov 20, 2003
  3. marek

    unicode regex example: trouble

    marek, May 21, 2004, in forum: Python
    Replies:
    1
    Views:
    363
    Peter Otten
    May 21, 2004
  4. Replies:
    3
    Views:
    749
    Reedick, Andrew
    Jul 1, 2008
  5. MRAB

    Re: trouble with regex?

    MRAB, Oct 8, 2009, in forum: Python
    Replies:
    1
    Views:
    351
    Paul McGuire
    Oct 8, 2009
Loading...

Share This Page