search and replace using reg expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Manjini
    New Member
    • Apr 2012
    • 1

    search and replace using reg expression

    I would like to search and replace an xml file with the ini content, but i am getting incorrect answer in my bb.xml with the same regular expression syntax without replacement.

    My ini file
    -----------
    myPara para
    lPara last_para
    <mySeca>([0-9]+). <sec id=\"$1\">

    My XML file.
    -----------
    Code:
    <myPara>dsa fhasdjkh fjaksdhkjfh asjkdhfjk lashdkjfh ajksdhf jkasdhfjka sd</myPara>
    <lPara>asdfh asgdhfgsdf asdhf asdhgf asd</lPara>
    <mySeca>1. asdfgashdgfhasdghfg sdhfg asd</mySeca>
    my perl part
    --------------
    Code:
    $filename = "c:\\temp\\temp\\F&R.ini";
    my $raw="c:\\temp\\temp\\aa.xml";
    my $output="c:\\temp\\temp\\bb.xml";
    open ($FIN,$raw) or die "Could not read from $raw, program halting.";
    my $instr;
    while (my $lines=<$FIN>)
    {
    	$instr.=$lines."";
    }
    open(FILE, $filename) or die "Could not read from $filename, program halting.";
    $count = 1;
    while (<FILE>)
    {
    chomp($_);
    my($find,$replace)=split(/\t/,$_,2);
    $instr=~s/$find/sub{$replace}->()/gei;
    count++;
    }
    open(FOUT,">$output"); print FOUT $instr; close FOUT;
    output bb.xml
    ------------
    Code:
    <para>dsa fhasdjkh fjaksdhkjfh asjkdhfjk lashdkjfh ajksdhf jkasdhfjka sd</para>
    <last_para>asdfh asgdhfgsdf asdhf asdhgf asd</last_para>
    <sec id=\"$1\">asdfgashdgfhasdghfg sdhfg asd</mySeca>
    Can anyone help me to solve this
    Last edited by numberwhun; Apr 26 '12, 08:34 PM. Reason: Please use Code Tags!!!
Working...