Regular Expression -- How to Configure for the User

Hi All,

I want to provide a configuration File to find
Find and Replace various pattern.

for example,

if the configuration like below:

###  Paramter = Search Pattern|Replace Pattern ##Comment for user

Parameter = <test1>all-text</test1>|<test2>all-text</test2>
Parameter = <test3>all-text</test3>|<test4>all-text</test4>

I need to transfer the,

   all-text in Search Pattern to (.*?);
   all-text in Replace Pattern to $1;

I am doing everything right....after printing the two Variable
$FindString and $ hold the correct pattern
like below:

$FindString = <test1>(.*?)</test1>
$ReplaceString = <test2>$1</test2>

When I have test this with my input like below:

$Line = "This is the example <test1>Sample text</test1> in this context";
$Line =~ s/$FindString/$ReplaceString/g;

But, when I printed the $Line varible the output
is like below:

"This is the example <test2>$1</test2> in this context";

how to change the my regular expression to
get <test2>Sample text</test2>....why it's retaining the $1 special variable there....!!

Any Help will be highly appreciated.

Who is Participating?
amitpagarwalConnect With a Mentor Commented:

Please check again, I ran the following code and it worked absolutely fine :

#!/usr/local/bin/perl5 -w
$Line = "This is the example <test1>Sample text</test1> in this context";
$Line =~ s/<test1>(.*?)<\/test1>/<test2>$1<\/test2>/g ;
print $Line ;
$ReplaceString = '"<test2>$1</test2>"';
$Line =~ s/$FindString/$ReplaceString/gee;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.