Hello everybody,

Can we perform substitution to the matched pattern inside a regular
expression so that the modified pattern gets returned instead of earlier
matched one ?

As a reference, in the following code below, I want to perform the
substitution of "~" character with "_" character to the value of "\3" inside
a regular expression so that $3 ultimately becomes "are___you___fine?"
instead of "are~~~you~~~fine?".

I tried checking with the perl docs but of no help. The only hope is using
"(?{})" which not only is experimental but also doesn't allow me to modify
the value of "\3" inside a regular expression.

Note : The reason why I want a solution entirely based on regular expression
because this regular expression will be used in a tool which supports usage
of perl regular expression inside its configuration file.

The source code as well as the output is mentioned below.

Please suggest.

==========================================================================================



[r...@host1 ~]#
[r...@host1 ~]# cat check.pl
#!/usr/bin/perl

use strict;
use warnings;

my $text1  = q/hello~~~how~~~are~~~you~~~fine?~~~OK/;
my $regex1 = qr/^([^\~]+)\~\~\~([^\~]+)(?:\~\~\~){0,1}(.*)\~\~\~([^\~]+)$/;

print "\n";
print "text1 is [$text1]\n\n";

print "regex1 is [$regex1]\n\n";

if ( $text1 =~ /$regex1/ )
{
        print "Regular expression matched\n\n";

        print "Field 1 : [$1]\n";
        print "Field 2 : [$2]\n";
        print "Field 3 : [$3]\n";
        print "Field 4 : [$4]\n";

        print "\n";
}
else
{
        print "Regular expressing didn't matched\n\n";
}
[r...@host1 ~]#
[r...@host1 ~]# perl check.pl

text1 is [hello~~~how~~~are~~~you~~~fine?~~~OK]

regex1 is [(?-xism:^([^~]+)~~~([^~]+)(?:~~~){0,1}(.*)~~~([^~]+)$)]

Regular expression matched

Field 1 : [hello]
Field 2 : [how]
Field 3 : [are~~~you~~~fine?]
Field 4 : [OK]

[r...@host1 ~]#


==========================================================================================



Thanks & Regards,
Amit Saxena

Reply via email to