Home > Software design >  combining multiple regular expression in perl [closed]
combining multiple regular expression in perl [closed]

Time:09-16

I have a bit of perl code where there are the following four regular expression matching statements:

$data =~ s/\,/\&/g;
$data =~ s/\;/\;/g;
$data =~ s/\s /\_/g;
$data =~ s/\|/\&/g;

These statements are part of a function that is called many times. My question is whether there is some compilation to do. I think probably perl takes care of it already as these are static and will not be compiled multiple times. Although, I am not sure...

My second question is whether there is some way to combine these into one statement. I wonder if that will result in shaving some computation time off.

CodePudding user response:

I think probably perl takes care of it already as these are static and will not be compiled multiple times.

That is correct. Because the pattern doesn't interpolate, it's compiled only once, when the program is compiled.

$ perl -Mre=debug -e'print("Start of execution\n"); /\w / for qw( a b );'
Compiling REx "\w "
...
Start of execution
Matching REx "\w " against "a"
...
Matching REx "\w " against "b"
...

My second question is whether there is some way to combine these into one statement. I wonder if that will result in shaving some computation time off.

It's only compiled once, but evaluated multiple times. This means it's evaluation time you want to cut, not compile time.

And yes, I think it's possible this code would be faster combined into a single s/// because this reduces the number of times the regex engine needs to be started up. I could be wrong. Try and see.

A hash would be useful here.

use feature qw( state );

state $repl = {
   "," => "&",
   "|" => "&",
   ";" => ";",
};

$data =~ s{([,|;]|\s )}{ $repl->{$1} // "_" }eg;

Of course, you want to build the hash only once, hout

  • Related