On Mon, 2008-09-01 at 11:40 -0400, Mr. Shawn H. Corey wrote:

> > use strict;
> > use warnings;
> > use diagnostics;
> > 
> > my $text="this is a random text. Please match random string.\nAnother
> > random something.\n";
> > 
> > my $rexp=<STDIN>;
> 
> chomp( $rexp );
> # I take it that you want $rexp to be
> # of the form: s/random/final/
> my $replacement = '';
> if( $rexp =~ /^s(.)/ ){
>   my $delimiter = $1;
>   ( undef, $rexp, $replacement ) = split /$delimiter/, $rexp;
> }else{
>   die "badly formatted substitution: $rexp";
> }
> 
> while( $text =~ s/$rexp/$replacement/g ){
> 
> >   my $l=length($`);
> >   print substr($text, 0, $l);
> >   print ">";
> >   print substr($text, $l);
> > }
> > 

Alright! This works... I solved the minor issue of modifiers and simple
matching instead of substitution (remember I need to take care of
*arbitrary* regexes) by:

my ($op, $replacement, $modifiers)=('', '');
if($rexp=~/^s(.)/){
   $op='s';
   my $delimiter=quotemeta($1);
  (undef, $rexp, $replacement, $modifiers) = split /$delimiter/, $rexp;
  if($modifiers) { $rexp = "(?$modifiers)$rexp";}
} else { ... # match other operations alike}

{no warnings 'regexp'; # this I need because I force /g... If user 
                       # enters /g as well, a warning (useless /g) is 
                       # raised.
  if($op eq 's') { #substitution... do something similar in 
                   # elsif branches for other ops
     while($text =~ s/$regex/$replacement/g){
        my $l=length($`);
        print substr($text, 0, $l);
        print ">";
        print substr($text, $l);
     }
  }
}

So this is fine. The minor issue I see is that this works great with all
delimiters but matching ones. Something in the lines of s(exp)(subs)
fails miserably... Still, I can survive with that.

Thanks a huge lot everyone... Hope to get good to this enough to be able
to help as you did with me soon enough :D

cheers
paolino

> > > Also see:
> > > 
> > > perldoc -q "How can I expand variables in text strings?"
> > 
> > oh btw, didn't know about -q... That's something cool!
> 
> `perldoc -q` searches only the FAQ.  See http://perldoc.perl.org/ for
> search engine that searches all of the perldoc's.  Make sure it is
> searching the correct version; to see what version of Perl you are
> running, enter:
> 
>   perl -v
> 
> 
> -- 
> Just my 0.00000002 million dollars worth,
>   Shawn
> 
> "Where there's duct tape, there's hope."
>         Cross Time Cafe
> 
> "Perl is the duct tape of the Internet."
>         Hassan Schroeder, Sun's first webmaster
> 
> 
-- 
Paolo Gianrossi
Softeco Sismat S.p.A. - R&D Division
via De Marini 1, 16149 Genova
Tel: +39 010 6026 332 - Fax: +39 010 6026 350
E-Mail: [EMAIL PROTECTED]




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to