Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Replacement in Regular Expression

Posted on 2007-11-28
4
Medium Priority
?
179 Views
Last Modified: 2010-04-21
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.
0
Comment
Question by:Aloha_Technology
4 Comments
 
LVL 5

Accepted Solution

by:
mkatmonkey earned 750 total points
ID: 20367342
This should work.

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

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
ID: 20367522
$welcome_string=~s/\%([\w_]+)\%/$params->{$1}/g;
0
 
LVL 85

Expert Comment

by:ozo
ID: 20369596
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
 

Author Closing Comment

by:Aloha_Technology
ID: 31411475
Yes, it works.

Thanks.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question