[Israel.pm] Regex Tips

Shmuel Fomberg semuelf at 012.net.il
Tue Feb 20 12:38:00 PST 2007

Hello There.

A few things that I didn't knew about regex:
1. When I do m/Literal/, Perl is running some very fast string search,
instead on a regex.

2. [AB] is faster then A|B.

3. when storing the regex in a scalar, and doing m/$regex/, Perl will
build a regex-machine every time it reaches that line. This can be very
wasteful, especially if it's inside a loop. To fix this I should do
$regex=qr/the regex/; and then the machine will be created in compile
3.1. doing m/$regex/o will create the machine only once per string, on
run-time. (I think)
3.2. Can be used to cause perl not to create machines on build time for
regex that probably won't be executed.

4. the i switch (m/.../i) kills most of the optimizations.

* Taken from a presentation by David Bird. 


More information about the Perl mailing list