perlquestion
bobf
<p>
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:
<code>
my $s = 'variable chars anchor want this';
</code>
I want to end with this:
<code>
$s = 'anchor want this';
</code>
</p>
<p>
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:
<ul>
<li>Capture the wanted portion</li>
<code>
if( $s =~ m/^.*?(anchor.*)$/ ){ $s = $1; }
</code>
<li>Capture and strip the variable portion</li>
<code>
if( $s =~ m/^(.*?)anchor/ ){ my $bad = $1; $s =~ s/$bad//; }
</code>
<code>
$s =~ s/^.*?anchor/anchor/;
</code>
<li>Silly constructs</li>
<code>
e.g., loops that utilize reverse, chop, split, ...
</code>
</ul>
</p>
<p>
All of these employ <c>.*</c> (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 ([id://24640]). 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 <c>anchor</c>".
</p>
<p>
How would you do this, and why?
</p>