Regular expressions are wonderful, but they are not the most efficient way to perform all string operations. Although regular expressions can be used to perform string operations like extracting substrings and translating characters , they are better suited for more complex operations. Simple string operations in Perl should be handled by special-purpose operators like index , rindex , substr , and tr/// . Bear in mind that all regular expression matches, even simple ones, have to manipulate memory variables. If all you need is a comparison or a substring, manipulating memory variables is a waste of time. For this reason, if no other, you should prefer special-purpose string operators to regular expression matches whenever possible. Compare strings with string comparison operatorsIf you have two strings to compare for equality, use string comparison operators, not regular expressions:
The string comparison operators are at least twice as fast as regular expression matches:
A few more complex comparisons are also faster if you avoid regular expressions:
Find substrings with index and rindexThe index operator locates an occurrence of a shorter string in a longer string. The rindex operator locates the rightmost occurrence, still counting character positions from the left:
The index operator is very fastit uses a Boyer-Moore algorithm for its searches. Perl will also compile index -like regular expressions into Boyer-Moore searches. You could write: Continued from above:
or, avoiding the use of $' (see Item 16 and Item 21):
However, the overhead associated with using a regular expression match makes index several times faster than m// for short strings. Extract and modify substrings with substrThe substr operator extracts a portion of a string, given a starting position and (optional) length:
The substr operator is much faster than a regular expression written to do the same thing (also see Item 19): Continued from above:
The nifty thing about substr is that you can make replacements with it by using it on the left side of an expression. The text referred to by substr is replaced by the string value of the right-hand side: Continued from above:
You can combine index and substr to perform s/// -like substitutions, but in this case s/// is usually faster:
You can also do other lvalue-ish things with a substr , such as binding it to substitutions or tr/// :
Transliterate characters with tr///Although it is possible to perform character-level substitutions with regular expressions, the tr/// operator provides a much more efficient mechanism: Use tr/// , not regular expressions, to transliterate characters.
The tr/// operator has other uses as well. It is the fastest way to count characters in a string, and it can be used to remove duplicated characters:
|