in reply to regexp find last word

Or eat more chars at the start.
$_ = "fox comes and fox goes into forest"; s/(.*)(fox.+?forest)/$1the $2/; print;
Boris

Replies are listed 'Best First'.
Re^2: regexp find last word
by holli (Abbot) on Mar 02, 2005 at 14:57 UTC
    Basically the same principle as my answer, but slightly slower.
    use Benchmark; print "holli\n"; timethis (999999, sub { $_ = "fox comes and fox goes into forest"; s/( +fox.+)(fox.+?forest)/${1}the $2/; }); print "borisz\n"; timethis (999999, sub { $_ = "fox comes and fox goes into forest"; s/( +.*)(fox.+?forest)/$1the $2/; }); print "sh1tn\n"; timethis (999999, sub { $_ = "fox comes and fox goes into forest"; s/( +?<=fox)(.+?)(fox)/$1the $2/; }); print "Roy Jonhson\n"; timethis (999999, sub { $_ = "fox comes and fox goes into forest"; s/( +?=fox(?:(?!.*fox).)*forest)/the /; });
    prints:
    holli timethis 999999: 10 wallclock secs ( 9.87 usr + 0.00 sys = 9.87 CPU) + @ 101275.98/s (n=999999) borisz timethis 999999: 14 wallclock secs (11.08 usr + 0.00 sys = 11.08 CPU) + @ 90268.91/s (n=999999) sh1tn timethis 999999: 12 wallclock secs (10.41 usr + 0.01 sys = 10.42 CPU) + @ 95959.98/s (n=999999) Roy Jonhson timethis 999999: 10 wallclock secs (10.19 usr + 0.02 sys = 10.20 CPU) + @ 98000.69/s (n=999999)
    /me wins ;-)


    holli, /regexed monk/

      That's all well for the speed, but let's check correctness:

      Testing string: fox comes and fox goes into forest

      Holli : fox comes and the fox goes into forest Roy : fox comes and the fox goes into forest Borisz : fox comes and the fox goes into forest shltn : fox comes and the fox goes into forest

      Every solution got that one right.

      Testing string: fox comes fox walks and fox goes into forest

      Holli : fox comes fox walks and the fox goes into forest Roy : fox comes fox walks and the fox goes into forest Borisz : fox comes fox walks and the fox goes into forest shltn : fox comes the fox walks and fox goes into forest

      Three foxes trip up shltn.

      Testing string: pig comes and fox goes into forest

      Holli : pig comes and fox goes into forest Roy : pig comes and the fox goes into forest Borisz : pig comes and the fox goes into forest shltn : pig comes and fox goes into forest

      And just one fox trips up both Holli and shltn

      So I'm just going to pretend I have judging power and disqualify Holli's and shltn's entries, which makes Roy Jonhson the new winner. :)

        Testing string: fox comes fox walks and fox goes into forest. no fox left
        holli: fox comes fox walks and the fox goes into forest. no fox +left borisz: fox comes fox walks and the fox goes into forest. no fox +left sh1tn: fox comes the fox walks and fox goes into forest. no fox +left Roy Johnson: fox comes fox walks and fox goes into forest. no fox left
        And that trips both sh1tn and Roy Johnson. Leaving borisz as the only correct solution.
      With corrected versions of yours and mine, plus ikegami's suggested alteration of mine:
      use strict; use warnings; use Benchmark 'cmpthese'; cmpthese( -2, { holli => sub { $_ = "fox comes and fox goes into forest"; s/(fox.+)?(fox.+?forest)/${1}the $2/; }, Roy => sub { $_ = "fox comes and fox goes into forest"; s/(?=fox(?:(?!fox).)*forest)/the /; }, ikegami => sub { $_ = "fox comes and fox goes into forest"; s/(fox(?:(?!fox).)*forest)/the $1/; }, });
      Rate ikegami holli Roy ikegami 34714/s -- -2% -27% holli 35541/s 2% -- -25% Roy 47659/s 37% 34% --

      Caution: Contents may have been coded under pressure.

      You can remove the (redundant) .* from Roy Johnson's.

      I don't know if it helps any, but you can remove the lookahead too:
      s/(fox(?:(?!fox).)*forest)/the $1/;

        I removed the redundant .* about 2 minutes after I posted it. People see things quick around here.

        Caution: Contents may have been coded under pressure.
      Here is what I mensure on my PPC:
      use strict; use warnings; use Benchmark 'cmpthese'; cmpthese( -2, { holli => sub { $_ = "fox comes and fox goes into forest"; s/(fox.+)?(fox.+?forest)/${1}the $2/; }, Roy => sub { $_ = "fox comes and fox goes into forest"; s/(?=fox(?:(?!fox).)*forest)/the /; }, ikegami => sub { $_ = "fox comes and fox goes into forest"; s/(fox(?:(?!fox).)*forest)/the $1/; }, borisz => sub { $_ = reverse "fox comes and fox goes into forest"; s/(tserof.*?)xof/${1}xof eht/; $_ = reverse; } } ); __OUTPUT__ Rate holli ikegami Roy borisz holli 62934/s -- -3% -25% -31% ikegami 65121/s 3% -- -23% -29% Roy 84099/s 34% 29% -- -8% borisz 91530/s 45% 41% 9% --