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:25
Subject:
Re: regex: how to I pick out items in the middle?
Message ID:
99ff3ee2-caf8-86c2-46ee-4125e6cf853c@zoho.com
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


Thank you!

 From my notes:

Picking out things in the beginning `^`, the middle `<(x ** 1)>`,
and the end `$`:

[0] > my Str $y="xxxxxx"; $y ~~ s/ .*? "x"/Q/; print "$y\n";
Qxxxxx

[0] > my Str $y="xxxxxx"; $y ~~ s/ ^ x ** 2 /Q/; print "$y\n";
Qxxxx

[0] > my Str $y="xxxxxx"; $y ~~ s/ ^ x ** 2  <(x ** 1)> /Q/; print "$y\n";
xxQxxx

[0] > my Str $y="xxxxxx"; $y ~~ s/ ^ x ** 2  <(x ** 3)> /Q/; print "$y\n";
xxQx

[0] > my Str $y="xxxxxx"; $y ~~ s/ x ** 2  $ /Q/; print "$y\n";
xxxxQ



Regex with "S" (capitol "S"):
    "S" does not alter the string, but returns the result.

    Note that you have to use "given" to make this work

[0] >  my Str $y="xxxxxx"; print S/^ x ** 2 /QQ/ given $y ~ "\n";
QQxxxx

[0] >  my Str $y="xxxxxx"; print S/^ [x ** 2] <(x ** 2)> /QQ/ given $y ~ 
"\n";
xxQQxx

[0] >  my Str $y="xxxxxx"; print ( S/ x ** 2 $/QQ/ given $y ) ~ "\n";
xxxxQQ

[0] >  my Str $y="xxxxxx"; print ( $y ~~ S/ x ** 2 $/QQ/ ) ~ "\n";
Potential difficulties:
     Smartmatch with S/// is not useful. You can use given instead: S/// 
given $foo
     ------>  my Str $y="xxxxxx"; print ( $y ~~ ⏏S/ x ** 2 $/QQ/ ) ~ "\n";
xxxxQQ




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