Link to home
Start Free TrialLog in
Avatar of Aloha_Technology
Aloha_Technology

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of mkatmonkey
mkatmonkey
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adam314
Adam314

$welcome_string=~s/\%([\w_]+)\%/$params->{$1}/g;
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
Avatar of Aloha_Technology

ASKER

Yes, it works.

Thanks.