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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$welcome_string=~s/\%([\w_ ]+)\%/$par ams->{$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
\w includes _
you don't need to \ the % > { or ' in this context
ASKER
Yes, it works.
Thanks.
Thanks.