regex - Combining a Variable and Regular Expression in Perl -


i have code:

$temp =~ s/\#name\#/$customername/g; 

where replacing #name# value in $customername using regex. want append variable onto end of name.

so want like:

$temp =~ s/\#name . $appendvalue\#/$customername/g; 

so be:

$temp =~ s/\#name_1\#/$customername/g; 

would work or there proper way handle this?


test cases:

  • hello #name#
  • this intended #name#

the pattern interpolates variables, no concatenation operator needed:

$temp =~ s/#name$appendvalue#/$customername/g; 

you might need protect special characters in variable, though, use \q...\e:

$temp =~ s/#name\q$appendvalue\e#/$customername/g; 

# not special in regex, no backslash needed (but doesn't hurt).


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -