Which is more efficient regular expression?

Posted by Vagnerr on Stack Overflow See other posts from Stack Overflow or by Vagnerr
Published on 2009-02-13T10:52:30Z Indexed on 2010/04/13 18:13 UTC
Read the original article Hit count: 243

Filed under:
|
|

I'm parsing some big log files and have some very simple string matches for example

if(m/Some String Pattern/o){
    #Do something
}

It seems simple enough but in fact most of the matches I have could be against the start of the line, but the match would be "longer" for example

if(m/^Initial static string that matches Some String Pattern/o){
    #Do something
}

Obviously this is a longer regular expression and so more work to match. However I can use the start of line anchor which would allow an expression to be discarded as a failed match sooner.

It is my hunch that the latter would be more efficient. Can any one back me up/shoot me down :-)

© Stack Overflow or respective owner

Related posts about perl

Related posts about regex