Regular Expression Problem...

Caleb Hattingh caleb1 at
Thu Dec 2 05:55:07 CET 2004

Obviously, Peter and Jorge are hardcore, but below is what a beginner like  
me hacked up:

My point, I guess, is that it is possible to quickly get a solution to a  
specific problem like this without being a total expert.  The code below  
was typed out once, and with only one minor correction before it produced  
the required behaviour.  Sure, it is not the general solution, but *this  
is WHY I use python* - You get a lot of mileage out of the basics.  Plus,  
I can still read and understand the sub-optimal code, and probably will be  
able to several years from now :)

Regular expressions are real powerful, and I am learning through using ViM  
- but it takes time and practice.

(err, I didn't include your example with quotes, but you should get the  
general idea)
'>>> stngs = [r'This Is An $EXAMPLE String', r'This Is An  
$EXAMPLE.String', r'This Is An $EXAMPLE',r'This Is An \$EXAMPLE\String']
'>>> stngs
['This Is An $EXAMPLE String', 'This Is An $EXAMPLE.String', 'This Is An  
$EXAMPLE', 'This Is An \\$EXAMPLE\\String']

'>>> for i in stngs:
	wdlist = i.split(' ')
	for j in wdlist:
		if j.find(r'$') > -1:
			dwd = j.split('.')[0]
			if dwd.find('\\') > -1:
				dwd = dwd.split('\\')[1]
			print dwd

On Wed, 01 Dec 2004 13:40:17 +0100, Peter Otten <__peter__ at> wrote:

> andrea.gavana at 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;
> ... """)
> Peter

More information about the Python-list mailing list