Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-15-2005, 04:04 PM   #1
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?

Old 06-16-2005, 11:12 AM   #2
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.
while(($key,$value) = each %hash){
        $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 11:13 AM.
Old 06-16-2005, 01:45 PM   #3
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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};

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

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 01:58 PM
Keeping part of a regexp in search/replace MadCactus Linux - General 4 09-29-2004 07:31 AM
regexp search for [ wijnands Linux - Newbie 3 06-22-2004 03:15 AM
perl regexp problem raven Programming 4 03-22-2004 12:49 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:00 PM.

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