in reply to Re: fast greedy regex
in thread fast greedy regex

unpack is worth a look

Sometimes, sometimes not. Unpack still has to reparse the format string each time it is called (unless the results are cached internally, but last time I looked they weren't). This cost can add up, which is why substr sometimes outperforms it.


One other remark to the OP:

Matching a user-agent string with /(\".*\")/ is pretty horrendous, but there's not much you can do, given that there are some more-or-less malicious useragent strings that contain " themselves. When I ran into this problem years ago the only elegant way I found to deal with it reliably was to walk forwards up to the opening double quote isolating the various fields, walk backwards from the end of the string isolating the other fields (the Referrer if memory serves correctly) and what remained was the User agent.

This can be done nicely with

$front = substr( $_, 0, index($_, '"' ) - 1, '' ); $back = substr( $_, rindex( $_, '"' ) + 1, '' ); $user_agent = $_; # modulo a quote or space or two

The above fragment is non-tested and may contain a fencepost error, but you get the idea. Once this is out of the way it should be possible to construct non-backtracking regexps to match what's left in $front and $back.

- another intruder with the mooring of the heat of the Perl