Rexexp on part of a string

Aaron Crane perl at aaroncrane.co.uk
Tue Feb 19 09:07:24 GMT 2008


Andrew Black writes:
> I would like to restrict the part of a string a regexp is allowed to
> operate on, For example I would like to ignore comments.  I would
> like
>
>   $x = " andrew andrew   # andrew " ;
>   $x =~ s/andrew/black/g ;
> to produce " black black   # andrew "

  my $comment_start_pos = index $x, '#';
  substr($x, 0, $comment_start_pos) =~ s/andrew/black/g;

-- 
Aaron Crane


More information about the london.pm mailing list