in reply to Get a known substring from a string
$1 is set to the part that matches the regex in between the brackets.if ($str =~ m/(thispart)/) { $found = $1; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Get a known substring from a string
by BrowserUk (Patriarch) on Sep 09, 2016 at 23:18 UTC | |
That is equivalent to $found = 'thispart' if 1+index( $str, 'thispart' );, but index is 3 times faster:
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
by Corion (Patriarch) on Sep 10, 2016 at 09:42 UTC | |
I know it looks really trivial once you see it, but I'm really astonished by your approach of using 1+index(...) - it had not occurred to me to use index that way in an expression to check for presence. I'll add that to my set of idiosyncratic phrases, just like if( system(...) == 0 ) { for successful execution of subprocesses. Update: I wondered about how much the capturing parentheses cost, and it seems they account for roughly Not invoking the regex engine still is much faster, even though I had thought there once was an optimization that turned constant regular expressions without anchors or quantifiers into an index lookup...
The program I used:
| [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Sep 10, 2016 at 11:40 UTC | |
I'm really astonished by your approach of using 1+index(...) - it had not occurred to me to use index that way in an expression to check for presence. I've been using it that way for as long as I can remember. This turns up 50+ of my uses here with the earliest being in September 2002 which is only a few months after I started coming here. Adding the one has another benefit when doing searches in a loop:
That of automatically moving the start point along after each match. You have to remember to subtract 1 when using the match point; but that's no more onerous than remembering to increment it. I had thought there once was an optimization that turned constant regular expressions without anchors or quantifiers into an index lookup... That optimisation is there, and can be even quicker than index but you have to code it exactly correctly for it to kick in::
Unfortunately, it doesn't generalise. Even using a variable instead of literal means some of that performance gain is lost:
The nature of the type of work I do means that I learnt early on to only start the regex engine if I needed regex. With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
by flowdy (Scribe) on Sep 13, 2016 at 07:34 UTC | |
by BrowserUk (Patriarch) on Sep 13, 2016 at 12:05 UTC | |
| |
by Anonymous Monk on Sep 13, 2016 at 09:16 UTC | |