Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Regex style and efficiency

by BrowserUk (Patriarch)
on Jan 10, 2010 at 22:13 UTC ( [id://816651]=note: print w/replies, xml ) Need Help??


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.

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://816651]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-28 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found