On May 5, 7:02 am, learn.tech...@gmail.com (Amit Saxena) wrote:
> On Fri, Apr 30, 2010 at 10:40 PM, C.DeRykus <dery...@gmail.com> wrote:
> > On Apr 30, 3:55 am, learn.tech...@gmail.com (Amit Saxena) wrote:
> > > 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
...
>
> Thanks Charles but this will not work as the tool only accepts perl regular
> expressions but not any perl code statements etc.
>

Is the regex an argument to the tool...? If so, could the
regex be modified as it's passed, eg,

some_tool.exe   do { $string =~ s{  (  (.)  \2 \2+ )   }
                                                 {  length($1) . $2 }
gex;
                             $string };

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to