Regular expressions help

M

Mike Hearne

Given the following three lines:

name fredgeorge
name georgeharry
name george

I'm trying to find a regular expression that matches only the third
one.

My book resources are pretty skimpy when it comes to regular
expressions, and the stuff on the web I've found doesn't cover this
circumstance, and I can't figure out how to extrapolate from what is
described to my problem.

Can anyone help?

Thanks!
 
M

Mike C. Fletcher

Look in the module reference for REs, particularly the expression syntax
section:
http://www.python.org/doc/2.3.3/lib/re-syntax.html

You probably want either the $ or \b constructs. I'll let you decide
which is more appropriate for your task. You might alternately want the
(?! ... ) construct, but that's probably more complex than your needs.

HTH,
Mike

Mike said:
Given the following three lines:

name fredgeorge
name georgeharry
name george

I'm trying to find a regular expression that matches only the third
one.
....
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
 
A

Andrei

Mike Hearne wrote on 24 Feb 2004 10:54:45 -0800:
Given the following three lines:

name fredgeorge
name georgeharry
name george

I'm trying to find a regular expression that matches only the third
one.

It's pretty easy:

"name george$"

"$" means end of string

--
Yours,

Andrei

=====
Real contact info (decode with rot13):
(e-mail address removed). Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
 
?

=?ISO-8859-1?Q?Bernard_Delm=E9e?=

name fredgeorge
name georgeharry
name george

I'm trying to find a regular expression that matches only the third
one.

'name george' would do, then ?!? If you mean 'george' on its
own as opposed to the previous variants, that'd be r'\bgeorge\b'.
One nifty tool to learn and experiment with re's is the
tools/scripts/redemo.py script normally present in your python installation.
 
A

Andrei

Bernard Delmée wrote on Tue, 24 Feb 2004 20:36:50 +0100:
'name george' would do, then ?!? If you mean 'george' on its

That one matches the "name george" part of "name georgeharry" as well,
which he didn't want.

--
Yours,

Andrei

=====
Real contact info (decode with rot13):
(e-mail address removed). Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
 
?

=?ISO-8859-1?Q?Bernard_Delm=E9e?=

That one matches the "name george" part of "name georgeharry" as well,
which he didn't want.

Indeed; my bad. I mostly wanted to mention redemo, for which
the windows installer should create a shortcut, really...
 
?

=?ISO-8859-1?Q?Bernard_Delm=E9e?=

That one matches the "name george" part of "name georgeharry" as well,
> which he didn't want.

Indeed; my bad. I mostly wanted to mention redemo, for which
the windows installer should create a shortcut, really...
 
J

John Hazen

Mike Hearne wrote on 24 Feb 2004 10:54:45 -0800:
* Andrei said:
It's pretty easy:

"name george$"

"$" means end of string

And if you'd also prefer not to match:

name name george

Then, you could match for:
"^name george$"

"^" means the beginning of the string.

-John
< my_first_name AT my_last_name DOT net >
 
M

Marcello Pietrobon

Bernard said:
Indeed; my bad. I mostly wanted to mention redemo, for which
the windows installer should create a shortcut, really...
Agreed
 

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
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top