[tpm] how to resubstitute a captured value

Dave Doyle dave.s.doyle at gmail.com
Fri Apr 19 08:27:27 PDT 2013


So long as start and end don't have to be regexes themselves:

#!/usr/bin/perl

use strict;

my ($start,$end) = ('-',':');

while (my $line = <DATA>) {
$line =~ s/^(?:\Q$start\E)?(.+?)(?:\Q$end\E)?$/$start$1$end/;
print $line;
}

__DATA__
stuff
-stuff
stuff:
-stuff:


--
dave.s.doyle at gmail.com


On 19 April 2013 11:16, Fulko Hew <fulko.hew at gmail.com> wrote:

> Thats what I started with, but...
> I forgot to mention that the match expression and replacement
> expresssion need to be inside variables
> ie.
>
> #!/usr/bin/perl
> use strict;
> my $match = '^-?(.+?):?$';
> my $replace = '-$1:';
>
>
> while (my $line = <DATA>) {
>     $line =~ s/$match/$replace/;
>
>     print $line;
> }
> __DATA__
> stuff
> -stuff
> stuff:
> -stuff:
>
>
> On Fri, Apr 19, 2013 at 11:07 AM, Dave Doyle <dave.s.doyle at gmail.com>wrote:
>
>> Not the most efficient, but you can just always remove and add them back:
>>
>> #!/usr/bin/perl
>>
>> use strict;
>>
>> while (my $line = <DATA>) {
>>  $line =~ s/^-?(.+?):?$/-$1:/;
>> print $line;
>> }
>>
>> __DATA__
>> stuff
>> -stuff
>> stuff:
>> -stuff:
>>
>>
>> --
>> dave.s.doyle at gmail.com
>>
>>
>> On 19 April 2013 10:58, Fulko Hew <fulko.hew at gmail.com> wrote:
>>
>>>  The subject line may not say it properly, but I need
>>> a _single_ (because of other constraints) s/// statement
>>> that effectively performs the following:
>>>
>>> If the string doesn't start with a hyphen and end with colon,
>>> then I want them added.
>>>
>>> ie:
>>>
>>> stuff   => -stuff:
>>> -stuff  => -stuff:
>>> stuff:  => -stuff:
>>> -stuff: => -stuff:
>>>
>>> _______________________________________________
>>> toronto-pm mailing list
>>> toronto-pm at pm.org
>>> http://mail.pm.org/mailman/listinfo/toronto-pm
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20130419/899e510c/attachment-0001.html>


More information about the toronto-pm mailing list