Newbie backreference question

P

paulm

Hi,
In perl I can do something like:

$a = 'test string';
$a =~ /test (\w+)/;
$b = $1;
print $b . "\n";

and my output would be "string".

How might this snippet be written in python?

Thanks to all...
 
L

Larry Bates

a='test string'
print a.split()[:-1]

I'm assuming that you want the last space separated word?

Larry Bates
 
R

Robert Kern

paulm said:
Hi,
In perl I can do something like:

$a = 'test string';
$a =~ /test (\w+)/;
$b = $1;
print $b . "\n";

and my output would be "string".

How might this snippet be written in python?

http://docs.python.org/lib/module-re.html

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
G

George Sakkis

Hi,
In perl I can do something like:

$a = 'test string';
$a =~ /test (\w+)/;
$b = $1;
print $b . "\n";

and my output would be "string".

How might this snippet be written in python?

Thanks to all...

import re
a = 'test string'
b = re.match(r'test (\w+)', a).group(1)
print b


George
 
P

paulm

Larry Bates said:
a='test string'
print a.split()[:-1]

I'm assuming that you want the last space separated word?

Larry Bates

Hi,
In perl I can do something like:

$a = 'test string';
$a =~ /test (\w+)/;
$b = $1;
print $b . "\n";

and my output would be "string".

How might this snippet be written in python?

Thanks to all...

No, sorry - my bad. I am looking to assign the
backreference to another variable so it can be treated
seperately. So perhaps:

$a = 'test string two';
$a =~ /test \w{2}([\W\w]+)/;
$b = $1;
print $b . "\n";

producing "ring two".

I have read the docs and faqs but remain too dense
to comprehend.

Thanks again...
 
G

George Sakkis

paulm said:
No, sorry - my bad. I am looking to assign the
backreference to another variable so it can be treated
seperately. So perhaps:

$a = 'test string two';
$a =~ /test \w{2}([\W\w]+)/;
$b = $1;
print $b . "\n";

producing "ring two".

I have read the docs and faqs but remain too dense
to comprehend.

Thanks again...

Did you have a look at my other reply ? It's still the same, just
change the regexp:

import re
a = 'test string two'
b = re.match(r'test \w{2}(.+)', a, re.DOTALL).group(1)
print b

By the way, if you want to catch any single character (including new
lines), '.' with re.DOTALL flag is more clear than [\W\w].

George
 
P

paulm

George Sakkis said:
Did you have a look at my other reply ? It's still the same, just
change the regexp:

import re
a = 'test string two'
b = re.match(r'test \w{2}(.+)', a, re.DOTALL).group(1)
print b

By the way, if you want to catch any single character (including new
lines), '.' with re.DOTALL flag is more clear than [\W\w].

George

Of course, this is the answer I need. I've been futzing
around with \1 and such without much luck. I'm still thinking a
bit too perlishly. :)

Thanks again, all!
 

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

Staff online

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top