develooper Front page | perl.perl6.users | Postings from October 2022

Re: regex: how to I pick out items in the middle?

Thread Previous | Thread Next
From:
ToddAndMargo via perl6-users
Date:
October 30, 2022 08:10
Subject:
Re: regex: how to I pick out items in the middle?
Message ID:
d046bfd8-d8a7-50cd-497b-adfa069c529f@zoho.com
On 10/29/22 22:37, William Michels via perl6-users wrote:
> 
> 
> On Sat, Oct 29, 2022 at 7:29 PM ToddAndMargo via perl6-users 
> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
> 
>      >> On Sat, Oct 29, 2022 at 6:46 PM ToddAndMargo via perl6-users
>      >> <perl6-users@perl.org <mailto:perl6-users@perl.org>
>     <mailto:perl6-users@perl.org <mailto:perl6-users@perl.org>>> wrote:
>      >>
>      >>     Hi All,
>      >>
>      >>     With a regex, how do I pick out items in the middle of the
>     string?  Two
>      >>     from the beginning or two from the end?
>      >>
>      >>
>      >>     4] > my Str $y="xxxxxx"; $y ~~ s/ $([.*-2]) "x"/Q/; print $y
>     ~ "\n"
>      >>
>      >>     ===SORRY!=== Error while compiling:
>      >>     Malformed postfix call
>      >>     ------> my Str $y="xxxxxx"; $y ~~ s/ $([.*⏏-2]) "x"/Q/;
>     print $y ~ "\n"
>      >>
>      >>
>      >>
>      >>     I am trying to do something like this:
>      >>
>      >>     [0] > my Str $x="1\n2\n3\n4\n5\n"
>      >>     1
>      >>     2
>      >>     3
>      >>     4
>      >>     5
>      >>
>      >>     [1] > print $x.lines[*-2] ~ "\n"
>      >>     4
> 
>     On 10/29/22 19:16, William Michels via perl6-users wrote:
>      > In the Raku REPL:
>      >
>      > $ raku
>      > Welcome to Rakudo™ v2022.07.
>      > Implementing the Raku® Programming Language v6.d.
>      > Built on MoarVM version 2022.07.
>      >
>      > To exit type 'exit' or '^D'
>      > [0] > #beginning
>      > Nil
>      > [1] > my Str $y="xxxxxx"; S/^ x ** 2 /QQ/.say given $y;
>      > QQxxxx
>      > [1] > #inner
>      > Nil
>      > [2] > my Str $y="xxxxxx"; S/^ [x ** 2] <(x ** 2)> /QQ/.say given $y;
>      > xxQQxx
>      > [2] > #end
>      > Nil
>      > [3] > my Str $y="xxxxxx"; S/ x ** 2 $/QQ/.say given $y;
>      > xxxxQQ
>      >
>      > HTH, Bill.
>      >
> 
>     Yes it does!  Thank you!
> 
>     What does capitol S do?
> 
> Quote:
> 
> |S///| uses the same semantics as the |s///| operator, except it leaves 
> the original string intact and /returns the resultant string/ instead of 
> |$/| (|$/| still being set to the same values as with |s///|).
> 
> https://docs.raku.org/syntax/S$SOLIDUS$SOLIDUS$SOLIDUS 
> <https://docs.raku.org/syntax/S$SOLIDUS$SOLIDUS$SOLIDUS>

And they give an example:

say S/o .+ d/new/ with 'old string';      # OUTPUT: «new string»

S:g/« (.)/$0.uc()/.say for <foo bar ber>; # OUTPUT: «Foo␤Bar␤Ber»

Which brings me back to the other of my criticisms
of the documentation.  The examples are high level
users showing off their skills making for a totally
useless example for beginners.  They should show a
simple example and then work up to the show off stuff.




Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About