LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-15-2005, 03:04 PM   #1
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
Perl Regexp search-n-replace


I have a hash of strings containing patterns (keys) and replacement strings (values). Normally, in perl, when you execute:

s/<regexp pattern>/repstr $1 $2/g

$1 and $2 will be replaced with whatever was matched in the regexp (provided the regexp contained parens for grouping). However when I place a replacement string such as 'repstr $1 $2' into the hash and then retrieve it to be placed into the s///g the $1,$2,... gets lost.

I have tried a couple different tricks to try to get 's///g' to evaluate the replacement string but I can't convince it. How do I fix this?

TIA,
jpbarto
 
Old 06-16-2005, 10:12 AM   #2
Chrax
Member
 
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Rep: Reputation: 31
This is a major problem with perl, one that is to be resolved in Perl6.

That said, I have a couple suggestions:
Have you tried backslashing the dollar signs? Then it won't (rather shouldn't... I haven't had this particular perl experience) evaluate them until you call it. But I doubt that will work. There is a way to evaluate perl code in a regular expression, but I can't recall it. I feel it was something like curly braces around the expression, but don't trust me there.

Next perhaps it would work to eval the entire expression.
Code:
while(($key,$value) = each %hash){
    eval{
        $string = s/$key/$value/g;
    }
}
Then again, I'm terrible at doing evals, so if that works char for char, I'll be damned.

Either way, assuming the hash is the only way to go, I think an eval of some sort is your best bet.

I'm sorry I'm so unhelpful. You really picked one of the aspects of perl that I can't quite get a hold on. Hopefully someone can come along who's better at those than I am.

Last edited by Chrax; 06-16-2005 at 10:13 AM.
 
Old 06-16-2005, 12:45 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
I'm sorry I'm so unhelpful
No, you have the idea

The script should be like :
PHP Code:
foreach $key (keys %hash) {
    
$replace $hash{$key};
    eval 
"\$string=~s/$key/$replace/g";

Note the "\$string" syntax so it does not try to replace
$string with its value

Last edited by keefaz; 06-16-2005 at 12:48 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to "Search & Replace" in html files using Perl? rebel Red Hat 8 04-09-2005 12:58 PM
Keeping part of a regexp in search/replace MadCactus Linux - General 4 09-29-2004 06:31 AM
regexp search for [ wijnands Linux - Newbie 3 06-22-2004 02:15 AM
perl regexp problem raven Programming 4 03-21-2004 11:49 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration