Regular Expression Problem...

Peter Otten __peter__ at web.de
Wed Dec 1 07:40:17 EST 2004


andrea.gavana at agip.it wrote:

> identifying/extracting a substring from another string. What I have to do
> is to extract all the strings that begins with a "$" character, but
> excluding characters like "." (point) and "'" (single quote) and "\" "/"
> (slashes). For example I have:
> 
> 1) This Is An $EXAMPLE String
> 2) This Is An $EXAMPLE.String
> 3) 'This Is An $EXAMPLE'
> 4) This Is An \$EXAMPLE\String;
> 
> I would like to extract only the "keyword" $EXAMPLE and what I'm using at

Is that what you want?

>>> import re
>>> r = re.compile("[$]\w+")
>>> r.findall("""
... 1) This Is An $EXAMPLE String
... 2) This Is An $EXAMPLE.String
... 3) 'This Is An $EXAMPLE'
... 4) This Is An \$EXAMPLE\String;
... """)
['$EXAMPLE', '$EXAMPLE', '$EXAMPLE', '$EXAMPLE']

Peter




More information about the Python-list mailing list