SPUG: multi-line substition, all at once.
John W. Krahn
jwkrahn at shaw.ca
Wed Jun 24 17:25:08 PDT 2009
Ryan Corder wrote:
> Greetings all,
Hello,
> I'm trying to do a multi-line substitution of a particular sort and things
> aren't quite working out. I've been grinding at it for a little bit now
> to the point that I think it's time to ask for some assitance. I'm using 5.10.
>
> Say I have the following:
>
> my $code=<<__CODE__;
> : sub hello {
> : say "Hello World!";
> : }
> __CODE__
>
> I would like to get back:
>
> [code] sub hello {
> say "Hello World!";
> }
> [/code]
>
> I've been playing around with the zero-width lookbehind assertions, but it
> seems as if the positive (?<=) and negative (?<!) behave differently. Both
> are supposed to make the match, but not include it in $&. Negative works
> that way, but positive is including or so it seems. Here is what I have so
> far...
>
> $code =~ s@(?:(?<=\s\:).*)+@"[code]$&[/code]"@esg; (gives me)
>
> :[code] sub hello {
> : say "Hello World!";
> : }
> [/code]
>
> $code =~ s@(^(\s\:)(.*)$)+@"[code]$3[/code]"@esg; (gives me)
>
> [code] sub hello {
> : say "Hello World!";
> : }
> [/code]
>
> Both are close in their own ways, but no cigar. I've tried other permutations
> of the regex to no avail.
>
> I know I could do this with two regex, but for reasons that are not pertinent
> to the discusion, I would really like to do this with one.
>
> Any hints?
$ perl -le'
my $code = <<__CODE__;
: sub hello {
: say "Hello World!";
: }
__CODE__
( $code = "[code]$code\[/code]" ) =~ s/ +://g;
print $code;
'
[code] sub hello {
say "Hello World!";
}
[/code]
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
More information about the spug-list
mailing list