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 > > { > > 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] > > Not exclusively a regex but here's an option: > > my $regex1 = qr/^( # field 1 - new capture > ([^~]+) # field 2 > ~~~ > ([^~]+) # field 3 > (?:~~~){0,1} > ) # end field 1 (needed for > length) > (.*) # target field 4 > ~~~ > ([^~]+)$ # final field 5 > /x; > if ( $text1 =~ /$regex1/ ) > { > ( my $target = $4 ) =~ tr/~/_/; > substr( $text1, length($1), length($4), $target ); # extra > substr arg > } > > orig: [hello~~~how~~~are~~~you~~~fine?~~~OK] > change: [hello~~~how~~~are___you___fine?~~~OK] > > > Perl 5.10 has named captures which'd make this more readable. > > -- > Charles DeRykus > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > Thanks Charles but this will not work as the tool only accepts perl regular expressions but not any perl code statements etc. Thanks anyway. Regards, Amit Saxena