Regular Expression Problem...

A

andrea.gavana

Hello NG,

I am quite new with Python... I'm writing an application that does
also some regexp things on strings, but I'm having problem about
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
the moment is:

#CODE BEGIN
import re

mystring = "This Is An \$EXAMPLE\String;"
regex = re.compile("[\$]+\S*",re.IGNORECASE)
keys = regex.findall(mystring)

#CODE END

Obviously this code returns things like $EXAMPLE', $EXAMPLE/, $EXAMPLE. and
so on...
Does anyone have a suggestion?

Thank you a lot.

Andrea.
 
P

Peter Otten

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
 
C

Caleb Hattingh

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

$EXAMPLE
$EXAMPLE
$EXAMPLE
$EXAMPLE
'>>>
***




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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,212
Messages
2,571,102
Members
47,698
Latest member
TerraT521

Latest Threads

Top