Yary,
I tried your examples out in REPL after reading Tobias and Timo's
responses.
After the second example, I tried outputting theĀ result of '$/' which
contains theĀ whole capture from the last regex match. My result was
> 'fosdffgg'.subst(/(f+)/,'( ' ~ $0 ~ ' )',:g)
( f )osd( f )gg
> $/
On Sun, 19 Apr 2020, yary wrote:
> How would one do s/(.+),(.+)/$1,$0/ using .subst ?
> -y
You can pass a code block as the second argument which assembles the
replacement text. The block is evaluated anew for every substitution
and it has access to the latest captures:
say .subst(/(.*) ','
How would one do s/(.+),(.+)/$1,$0/ using .subst ?
-y
On Sun, Apr 19, 2020 at 6:21 PM Tobias Boege wrote:
> On Sun, 19 Apr 2020, yary wrote:
> > Question from today's Raku meetup. This works in a way I expect
> >
> > > 'fosdffgg'.subst(/f+/,"( "~ * ~" )", :g);
> > ( f )osd( ff )gg
> >
> > This
On Sun, 19 Apr 2020, yary wrote:
> Question from today's Raku meetup. This works in a way I expect
>
> > 'fosdffgg'.subst(/f+/,"( "~ * ~" )", :g);
> ( f )osd( ff )gg
>
> This one, $0 gets the single f each time
>
> > 'fosdffgg'.subst(/(f+)/,"( $0 )", :g);
> ( f )osd( f )gg
>
> Bug or
Hi Yary,
in the first example you've got a WhateverCode, but in the second one
you're actually just passing a string to subst, i.e. you're evaluating
$0 before subst is even called.
Check this as well:
> raku -e '"whoa" ~~ /(.)/; say \'fosdffgg\'.subst(/(f+)/,"( $0 )", :g)'
> ( w )osd( w )gg
Question from today's Raku meetup. This works in a way I expect
> 'fosdffgg'.subst(/f+/,"( "~ * ~" )", :g);
( f )osd( ff )gg
This one, $0 gets the single f each time
> 'fosdffgg'.subst(/(f+)/,"( $0 )", :g);
( f )osd( f )gg
Bug or misunderstanding on my part?
-y