Trouble with regex
Jim Shapiro
jim at jimshapiro.com
Fri Nov 14 10:20:27 EST 2003
"Fernando Rodriguez" <frr at easyjob.net> wrote in message
news:52p9rvs6av0dqe59t733o5netcd9t6r7q2 at 4ax.com...
> 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
More information about the Python-list
mailing list