Replacement in Regular Expression

Hello Experts,

I have a problem in using regular expression in Perl.
I am fetching a value from database and storing it in a variable '$welcome_string'.
The value fetched is unknown, but it contains value something like,

'Welcome %FIRST_NAME% %LAST_NAME% to this portal'.

I want a regular expression which will convert the above string to the following:

'Welcome $params->{'FIRST_NAME'} $params->{'LAST_NAME'} to this portal'.

Kindly provide solution.

Thanks.
Aloha_TechnologyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mkatmonkeyConnect With a Mentor Commented:
This should work.

$welcome_string=~s/\%(\w+)\%/\$params-\>\{'\1\'}/g

Open in new window

0
 
Adam314Commented:
$welcome_string=~s/\%([\w_]+)\%/$params->{$1}/g;
0
 
ozoCommented:
When you say $params->{'FIRST_NAME'} do you mean that literal string, or do you want the value contained in the hash ref?

\w includes _

you don't need to \ the % > { or '  in this context
0
 
Aloha_TechnologyAuthor Commented:
Yes, it works.

Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.