bobf has asked for the wisdom of the Perl Monks concerning the following question:
I have a string that begins with a variable sequence of characters, which is followed by a constant string that can be used as a unique anchor. I want to strip off the variable stuff at the beginning so that it begins with the anchor. For example, given this:
I want to end with this:my $s = 'variable chars anchor want this';
$s = 'anchor want this';
I can think of several ways to approach this, but I do not know which might be considered better than others from the standpoint of style or efficiency (not just benchmarking, but also including potential for backtracking). These approaches include:
- Capture the wanted portion
if( $s =~ m/^.*?(anchor.*)$/ ){ $s = $1; }
if( $s =~ m/^(.*?)anchor/ ){ my $bad = $1; $s =~ s/$bad//; }
$s =~ s/^.*?anchor/anchor/;
e.g., loops that utilize reverse, chop, split, ...
All of these employ .* (except for the silly constructs, which are left as an exercise for the reader), but I am trying to avoid that idiom by thinking more carefully about my regexen (Death to Dot Star!). I suspect a better regex might be something like a negated character class, but for a group: "match everything from the start of the string that does not match the sequence of characters anchor".
How would you do this, and why?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Regex style and efficiency
by BrowserUk (Patriarch) on Jan 10, 2010 at 22:13 UTC | |
by bobf (Monsignor) on Jan 10, 2010 at 22:23 UTC | |
by ikegami (Patriarch) on Jan 11, 2010 at 01:04 UTC | |
Re: Regex style and efficiency
by JavaFan (Canon) on Jan 11, 2010 at 10:02 UTC |