Rexexp on part of a string

virtualsue virtuallysue at gmail.com
Tue Feb 19 11:21:40 GMT 2008


Mike Whitaker wrote:
> 
> On 19 Feb 2008, at 06:35, Andrew Black wrote:
> 
>> 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 "
> 
> 
> TMTOWDTI :)
> 
> you could simply split() on #, s/andrew/black/g and rejoin
> or use a lookahead
> s/andrew(?=.*\#)/black/g;

If I split on # then what happens if $x contains 'print "==#andrew#andrew#===\n"  # more andrew'?

Maybe the data would never look like that, but calling them comments and using the '#' to delineate 
them makes you wonder.




More information about the london.pm mailing list