in reply to Regex style and efficiency

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.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^2: Regex style and efficiency
by bobf (Monsignor) on Jan 10, 2010 at 22:23 UTC

    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!

Re^2: Regex style and efficiency
by ikegami (Patriarch) on Jan 11, 2010 at 01:04 UTC
    As is, the substr/index version assumes the anchor is present. That may or may not be a problem.