I have been trying a while to get this working but not having luck. Here is my text file (first.txt)
<metric>
<baseFilter>
<and>
<or>
<value field="id">1111</value>
<value field="id">2222</value>
</or>
<or>
<value field="resolution" />
</or>
</metric>
I want to replace the strings between the first "or" and "/or" with these strings which is second text file (second.txt). I can have 50 or more value field lines between the first "or" and "/or", hence, i am searching for strings between "or" and "/or" and replacing with whatever in second.txt.
<value field="id">3333</value>
<value field="id">4444</value>
Expected output:
<metric>
<baseFilter>
<and>
<or>
<value field="id">3333</value>
<value field="id">4444</value>
</or>
<or>
<value field="resolution" />
</or>
</metric>
I have got the following perl code for that.
#!/usr/bin/perl
my $first = 'first.txt';
open (my $fh, '<', $first) or die "cannot open file $first";
{
local $/;
$first = <$fh>;
}
$find = "([\s]+)(<or>)([\n\r\s]+).*(\n|.)+?([\n\r\s]+)(<\/or>)";
my $content = 'second.txt';
open (my $fh, '<', $content) or die "cannot open file $content";
{
local $/;
$content = <$fh>;
}
$first =~ s/$find/$1$2$3$content$5$6/;
print "After sub First is $first\n\n";
When I run my code, the substitution is not happening and my $first remains the same, ie, first.txt appears again. What am I missing ? I used my regex in an online regex tester like http://www.regexr.com/, my regex matches the multi-line string between the first "or" and "/or". Why is perl not liking my regex ?
#<value field="id">.*?</value>\s+<value field="id">.*?</value>#mand replace that?