Solved

Replacement in Regular Expression

Posted on 2007-11-28
4
170 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 250 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 84

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SIMPLE Perl Regex 1 157
Trying to use two loops in a perl script but not getting results need. 10 88
instmodsh - listing perl modules 2 78
Call Shell Script from Perl Script 6 97
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

806 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