Solved

Replacement in Regular Expression

Posted on 2007-11-28
4
169 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Uploading Binary Data using Perl 5 83
Exchange 2010 Transport Rule Regex 28 95
batch script for automated email 12 101
Vb script to unzip a files and rename the files 12 97
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now