Regular expressions help

Discussion in 'Python' started by Mike Hearne, Feb 24, 2004.

  1. Mike Hearne

    Mike Hearne Guest

    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!
     
    Mike Hearne, Feb 24, 2004
    #1
    1. Advertising

  2. 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 Hearne wrote:

    >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/
     
    Mike C. Fletcher, Feb 24, 2004
    #2
    1. Advertising

  3. Mike Hearne

    Andrei Guest

    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):
    . Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
    gur yvfg, fb gurer'f ab arrq gb PP.
     
    Andrei, Feb 24, 2004
    #3
  4. > 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.
     
    =?ISO-8859-1?Q?Bernard_Delm=E9e?=, Feb 24, 2004
    #4
  5. Mike Hearne

    Andrei Guest

    Bernard Delmée wrote on Tue, 24 Feb 2004 20:36:50 +0100:

    >> 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


    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):
    . Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
    gur yvfg, fb gurer'f ab arrq gb PP.
     
    Andrei, Feb 24, 2004
    #5
  6. > 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?=, Feb 24, 2004
    #6
  7. > 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?=, Feb 24, 2004
    #7
  8. Mike Hearne

    John Hazen Guest

    > 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.


    * Andrei <> [2004-02-24 12:34]:
    > 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 >
     
    John Hazen, Feb 26, 2004
    #8
  9. Bernard Delmée wrote:

    >> 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...
    >

    Agreed
     
    Marcello Pietrobon, Mar 2, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris R. Timmons

    Re: Help with regular expressions.

    Chris R. Timmons, Jul 4, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    408
    Chris R. Timmons
    Jul 4, 2003
  2. David Waz...

    Re: Help with regular expressions.

    David Waz..., Jul 4, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    394
    David Waz...
    Jul 4, 2003
  3. Stephajn Craig

    Regular Expressions....HELP!

    Stephajn Craig, Jul 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    3,041
    Cowboy \(Gregory A. Beamer\)
    Jul 16, 2003
  4. Jay Douglas
    Replies:
    0
    Views:
    627
    Jay Douglas
    Aug 15, 2003
  5. Noman Shapiro
    Replies:
    0
    Views:
    245
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page