Just another Perl shrine | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Here's one rule you might like: It is almost always a mistake to use the non-greedy regex quantifiers like
*? and +?.
Typical code looks like this:
The purpose of the (.*?) is to capture a quoted part of the string in $churchill. Beginners usually try (.*) which doesn't work, because it captures everything from the first quotation mark to the last, including the non-quoted part in between. So then they ask how to fix this and are advised to use (.*?) instead. This does work, but it's much slower than it needs to be. A faster solution is:
This says that what you're interested in is a quote character, followed by a sequence of characters that are not quotes ([^"]) followed by another quote. The description of what you want is more exact, and it enables Perl to do the matching more efficiently. So a good rule of thumb is to avoid .*? wherever possible and to use something like [^"]* instead when you can.
In reply to Re: Efficient Perl Programming
by Dominus
|
|