Substitue and matched expression in a regex perl
-
20-06-2021 - |
Question
I have a HTML file which has many expression like
/v/global/nagargaurav/CALL::ROWS_ARE_MATCHED.log
I have my code like this
my $dir = /v/global/nagargaurav/;
$html =~ s/$dir\\/(.*?)::(.*?)\.log/$1 : $2/g;
print $html;
it gives the output -
CALL : ROWS_ARE_MATCHED
But what i need is to output like this
**CALL : ROWS ARE MATCHED**
and there are many such lines but the expression is same. can anybody help me with this.
Solution
use strict;
use warnings;
my $html = '/v/global/nagargaurav/CALL::ROWS_ARE_MATCHED.log';
my $dir = '/v/global/nagargaurav/';
$html =~ s/$dir\/(.*?)::(.*?)\.log/$1 : $2/g;
$html =~ s/_/ /g;
print $html;
OTHER TIPS
Possible in one step by using /e:
$html =~ s{ $dir /? ([^:]+) :: ([^.]+) \.log }
{ do{ local$_=$2;y/_/ /;"$1 : $_"} }gsex;
Result:
CALL : ROWS ARE MATCHED
BTW, your declarations are not Perl, these should read:
my $html = '/v/global/nagargaurav/CALL::ROWS_ARE_MATCHED.log';
my $dir = '/v/global/nagargaurav/';
or similar.
Regards
rbo
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow