Perl regex inside regex

I would like to ask if it is possible to add another regular expression inside the RHS wildcard matching expression using the modifier "e".

For example, I would like to replace any occurrence of a word stored in $ foo, with the same number "-" , case insensitive.

For instance:

$str =~ s/($foo)/$temp = $1; $temp ~= s/./-//gie;

But it constantly gives a syntax error when compiling, and

$str =~ s/($foo)/$temp = $1; $temp = "---"/gie; 

really works.

I believe that I did not correctly avoid slashes, any ideas?

+4
source share
2 answers

You probably need a modifier ethat can use

$str = 'banana';
$foo = 'na';
$str =~ s/$foo/$&=~s#.#-#gr/ge;
print $str;

. - Perl

, / regex, ( ).

e , r , , .

, Perl v.5.20 $& (...):

$str =~ s/($foo)/$1=~s#.#-#gr/ge;
          ^    ^ ^^ 
+6

s/// s///

, , :

$_ = "footastic Funtastic Fantastic";
my $foo = "f..";
s/($foo)/'-' x length($1)/gie;
print;  # ---tastic ---tastic ---tastic
0

Source: https://habr.com/ru/post/1658862/


All Articles