Regular expressions help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Hearne

    Regular expressions help

    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!
  • Andrei

    #2
    Re: Regular expressions help

    Mike Hearne wrote on 24 Feb 2004 10:54:45 -0800:
    [color=blue]
    > 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.[/color]

    It's pretty easy:

    "name george$"

    "$" means end of string

    --
    Yours,

    Andrei

    =====
    Real contact info (decode with rot13):
    cebwrpg5@jnanqb b.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
    gur yvfg, fb gurer'f ab arrq gb PP.

    Comment

    • Bernard Delmée

      #3
      Re: Regular expressions help

      > name fredgeorge[color=blue]
      > name georgeharry
      > name george
      >
      > I'm trying to find a regular expression that matches only the third
      > one.[/color]

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

      Comment

      • Andrei

        #4
        Re: Regular expressions help

        Bernard Delmée wrote on Tue, 24 Feb 2004 20:36:50 +0100:
        [color=blue][color=green]
        >> name fredgeorge
        >> name georgeharry
        >> name george
        >>
        >> I'm trying to find a regular expression that matches only the third
        >> one.[/color]
        >
        > 'name george' would do, then ?!? If you mean 'george' on its[/color]

        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):
        cebwrpg5@jnanqb b.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
        gur yvfg, fb gurer'f ab arrq gb PP.

        Comment

        • Bernard Delmée

          #5
          Re: Regular expressions help

          > That one matches the "name george" part of "name georgeharry" as well,[color=blue]
          > which he didn't want.[/color]

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

          Comment

          • Mike C. Fletcher

            #6
            Re: Regular expressions help

            Look in the module reference for REs, particularly the expression syntax
            section:
            The official home of the Python Programming Language


            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:
            [color=blue]
            >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.
            >
            >[/color]
            ....
            _______________ _______________ _________
            Mike C. Fletcher
            Designer, VR Plumber, Coder




            Comment

            • Bernard Delmée

              #7
              Re: Regular expressions help

              > That one matches the "name george" part of "name georgeharry" as well,[color=blue]
              > which he didn't want.[/color]

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

              Comment

              • John Hazen

                #8
                Re: Regular expressions help

                > Mike Hearne wrote on 24 Feb 2004 10:54:45 -0800:[color=blue]
                >[color=green]
                > > 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.[/color][/color]

                * Andrei <fake@fake.ne t> [2004-02-24 12:34]:[color=blue]
                > It's pretty easy:
                >
                > "name george$"
                >
                > "$" means end of string[/color]

                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 >

                Comment

                • Marcello Pietrobon

                  #9
                  Re: Regular expressions help

                  Bernard Delmée wrote:
                  [color=blue][color=green]
                  >> That one matches the "name george" part of "name georgeharry" as well,
                  >> which he didn't want.[/color]
                  >
                  >
                  > Indeed; my bad. I mostly wanted to mention redemo, for which
                  > the windows installer should create a shortcut, really...
                  >[/color]
                  Agreed


                  Comment

                  Working...