Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Regex style and efficiency

by bobf (Monsignor)
on Jan 10, 2010 at 22:07 UTC ( #816649=perlquestion: print w/replies, xml ) Need Help??

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:

my $s = 'variable chars anchor want this';
I want to end with 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; }
  • Capture and strip the variable portion
  • if( $s =~ m/^(.*?)anchor/ ){ my $bad = $1; $s =~ s/$bad//; }
    $s =~ s/^.*?anchor/anchor/;
  • Silly constructs
  • 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

    Because it is simple and efficient:

    $s = 'variable chars anchor want this';; $s = substr $s, index $s,'anchor';; print $s;; anchor want this

    If I was going to use a regex (say the anchor had a variable component), then:

    $s = 'variable chars anchor want this';; $s =~ s[.+(?=anchor)][];; print $s;; anchor want this

    The difference:

    cmpthese -1,{ a=> q[$s='variable chars anchor want this';$s=~s[.+(?=anchor)][];] +, b=> q[$s='variable chars anchor want this';$s=substr $s, index $s, +'anchor';] };; Rate a b a 1592446/s -- -48% b 3055291/s 92% --

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Nice. It didn't even occur to me to use substr/index for this. I also like how you used the non-capturing look-ahead to prevent the desired text from being included in the s///. Thanks!

      As is, the substr/index version assumes the anchor is present. That may or may not be a problem.
Re: Regex style and efficiency
by JavaFan (Canon) on Jan 11, 2010 at 10:02 UTC
    Here's one that doesn't use .*:
    $s =~ /anchor/p and $s = ${^MATCH} . ${^POSTMATCH};

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://816649]
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2023-02-04 22:08 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (31 votes). Check out past polls.