in reply to replace / with \ using regular expressions?
For replacing a single, known character, then regular expressions are the wrong tool. You need the tr/// operator as has already been pointed out.
The difference in performance is impressive:
$ cat bench #!/usr/bin/perl use strict; use warnings; use Benchmark 'cmpthese'; sub regex_subst { $_ = 'this/is/a/string'; s|/|\\|g; } sub tr_subst { $_ = 'this/is/a/string'; tr|/|\\|; } cmpthese(1_000_000, { s => \®ex_subst, tr => \&tr_subst, }); $ ./bench Rate s tr s 287356/s -- -75% tr 1162791/s 305% -- $
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: replace / with \ using regular expressions?
by Juerd (Abbot) on Jun 21, 2009 at 23:09 UTC | |
|
Re^2: replace / with \ using regular expressions?
by Anonymous Monk on Jun 20, 2009 at 14:22 UTC | |
by Marshall (Canon) on Jun 20, 2009 at 15:40 UTC |