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

  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 C. Fletcher
    Designer, VR Plumber, Coder
    http://members.rogers.com/mcfletch/
     
    Mike C. Fletcher, Feb 24, 2004
    #2
    1. Advertisements

  3. Mike Hearne

    Andrei Guest

    Mike Hearne wrote on 24 Feb 2004 10:54:45 -0800:
    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 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:
    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,
    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,
    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:
    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. Agreed
     
    Marcello Pietrobon, Mar 2, 2004
    #9
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.