regex - backwards?

Discussion in 'Perl Misc' started by John, Oct 7, 2009.

  1. John

    John Guest

    Hi

    $x =~s |a|b|;

    That will replace the first 'a' by 'b' in $x.

    Is there any obvious tweek so that it replaces the last occurrence of 'a'?

    Regards
    John
     
    John, Oct 7, 2009
    #1
    1. Advertising

  2. "John" <> wrote:
    >$x =~s |a|b|;
    >
    >That will replace the first 'a' by 'b' in $x.
    >
    >Is there any obvious tweek so that it replaces the last occurrence of 'a'?


    The most simple solutions are often the best solutions:
    reverse;
    s///;
    reverse;
    As long as you don't do that hundreds of thousands of times or you are
    dealing with gigantic strings there shouldn't be much of a problem with
    this trivial approach.

    jue
     
    Jürgen Exner, Oct 7, 2009
    #2
    1. Advertising

  3. John

    Danny Woods Guest

    "John" <> writes:

    > Hi
    >
    > $x =~s |a|b|;
    >
    > That will replace the first 'a' by 'b' in $x.
    >
    > Is there any obvious tweek so that it replaces the last occurrence of 'a'?


    Yes: regex lookahead:

    $x =~ s|a(?=[^a]*$)|b|;

    Can be read as "an 'a', followed by zero or more non-'a's up until the
    end of line"

    Cheers,
    Danny.
     
    Danny Woods, Oct 7, 2009
    #3
  4. John

    Jim Gibson Guest

    In article <hahfd0$lc5$>, John <>
    wrote:

    > Hi
    >
    > $x =~s |a|b|;
    >
    > That will replace the first 'a' by 'b' in $x.
    >
    > Is there any obvious tweek so that it replaces the last occurrence of 'a'?


    $x =~ s/(.*)a/$1b/;

    should do it, taking advantage of the fact that the * modifier is
    greedy.

    --
    Jim Gibson
     
    Jim Gibson, Oct 7, 2009
    #4
  5. John

    Guest

    On Wed, 7 Oct 2009 08:19:00 +0100, "John" <> wrote:

    >Hi
    >
    >$x =~s |a|b|;
    >
    >That will replace the first 'a' by 'b' in $x.
    >
    >Is there any obvious tweek so that it replaces the last occurrence of 'a'?
    >
    >Regards
    >John
    >
    >


    If you don't want to use lookahead as Danny suggested,
    which is probably easiest, a couple of alternatives:

    $aaa =~ s/a ([^a]*) $ /b$1/x;
    # or
    substr ($aaa, pos($aaa)-1, 1) = 'b' if ($aaa =~ / .*a /xg);

    Of these, lookahead may be quickest. Have to benchmark them.

    -sln
     
    , Oct 7, 2009
    #5
    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. Bill Priess
    Replies:
    0
    Views:
    421
    Bill Priess
    Aug 5, 2003
  2. John Saunders
    Replies:
    0
    Views:
    484
    John Saunders
    Jul 28, 2004
  3. Cowboy \(Gregory A. Beamer\) [MVP]

    Re: How do I Stop the Backspace from Navigating Backwards?

    Cowboy \(Gregory A. Beamer\) [MVP], Jul 28, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    387
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jul 28, 2004
  4. Replies:
    5
    Views:
    857
  5. Replies:
    3
    Views:
    781
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page