rovf has asked for the wisdom of the Perl Monks concerning the following question:
Consider for example a string $str="xyabcyxyxyxyxy" and a string $chompstr="xy". I would like to produce a new string which as many copies of $chompstr chopped off at the end as possible - in the example case, the result would be "xyabcy". What would be a good way (in whatever measurement of the word "good") to solve this?
My first attempt was to use the following regexp:
I used \z instead of $, because we might legally chomp something like "#\n". This works most of the time, but it does of course not work if $chompstr contains characters which have special meanings in a regexp. So I was thinking about the following possibilities:$str =~ s/($chompstr)*\z//;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Chomping Frenzy question
by moritz (Cardinal) on Jul 10, 2008 at 13:09 UTC | |
by rovf (Priest) on Jul 10, 2008 at 14:14 UTC | |
by moritz (Cardinal) on Jul 10, 2008 at 14:24 UTC | |
by ikegami (Patriarch) on Jul 10, 2008 at 18:40 UTC | |
|
Re: Chomping Frenzy question
by poolpi (Hermit) on Jul 10, 2008 at 13:57 UTC | |
by rovf (Priest) on Jul 10, 2008 at 14:18 UTC | |
by linuxer (Curate) on Jul 10, 2008 at 19:32 UTC | |
by rovf (Priest) on Jul 11, 2008 at 08:57 UTC |