Regular Expressions   «Prev  Next»
Lesson 8Perl e modifier
Objective e modifier is placed at the end of the substitution expression.

Perl e modifier

The e modifier is used when you want the right-hand side of the substitution expression to be evaluated as code, instead of being treated as a string.
For example,
s/$(\w+)//e;

If there is a need to substitute the substring with a replacement string which is a regular expression to be evaluated, the 'e' modifier is used. The 'e' modifier is placed at the end of the substitution expression.
s/To_be_replaced/Regular_Expression/e;

That will replace anything that looks like a Perl scalar variable with the contents of that variable. This is extremely useful for embedding values in a text file. Let us look at this example more closely:



1) The user is referring to a variable name $money
1) The user is referring to a variable name $money. so the substitution must evaluate this as code instead of as a string.

2) This line of code will do the substitution
2) This line of code will do the substitution

3) Matches the dollar sign in before money
3) Matches the dollar sign in before money

4) matches any word that comes after the dollar sign.
4) matches any word that comes after the dollar sign. The brackets tell the Perl program to remember what matched the \w+ and stores that in the variable $l.


5) refers to the first item in parentheses in a matching string
5) refers to the first item in parentheses in a matching string

6) makes the item a scalar variable
6) makes the item a scalar variable

7) tells the Perl program to treat the resulting string as a Perl variable, not a literal string
7) tells the Perl program to treat the resulting string as a Perl variable, not a literal string

8) The result is the Perl variable money
8) The result is the Perl variable money


Regular Expressions

Perl e Substitution Modifier

The e modifier is used if you want the right-hand side of the substitution to be evaluated as code, instead of being treated as a string. For example,
s/$(\w+)//e;

That will replace anything that looks like a Perl scalar variable with the contents of that variable. This is extremely useful for embedding values in a text file. Let us look at this example more closely:

Converting number formats

Sometimes the unstructured data that you receive will contain numerical data and the only changes that you will want to make are to reformat the numbers into a standardized format. This breaks down into two processes.
  1. First you have to recognize the numbers you are interested in, then
  2. you need to reformat them.

  • Recognizing numbers
    How do you recognize a number? The answer depends on what sort of numbers you are dealing with. Are they integers or floating points? Can they be negative? Do you accept exponential notation (such as 1E6 for 1 × 106)?
    When you answer these questions, you can build a regular expression that matches the particular type of number that you need to process.

To match natural numbers (i.e., positive integers) you can use a simple regular expression such as:
/\d+/

To match integers (with optional +/- signs) use
/[-+]?\d+/

To match a floating point number use
/[-+]?(\d+(\.\d*)?|\.\d+)/

To match a number that can optionally be in exponential notation, use
/[-+]?(?=\d|\.\d)\d*(\.\d*)?([eE]([-+]?\d+))?/

As these become rather complex, it might be a suitable time to consider using Perl's precompiled regular expression feature and creating your number-matching regular expressions in advance. You can do something like this:
my $num_re = qr/[-+]?(?=\d|\.\d)\d*(\.\d*)?([eE]([-+]?\d+))?/;
my @nums;
while ($data =~ /$num_re/g) {
  push @nums, $1;
}

to print out a list of all of the numbers in $data.
If you have a function, reformat, that will change the numbers into your preferred format then you can use code like this:
$data =~ s/$num_re/reformat($1)/ge;

which makes use, once more, of the e modifier to execute the replacement string before using it.

SEMrush Software