On Sat, Oct 29, 2022 at 7:29 PM ToddAndMargo via perl6-users <
perl6-us...@perl.org> wrote:

> >> On Sat, Oct 29, 2022 at 6:46 PM ToddAndMargo via perl6-users
> >> <perl6-us...@perl.org <mailto:perl6-us...@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

If you try to smartmatch with S///, you'll see the following error:

Potential difficulties:
    Smartmatch with S/// is not useful. You can use given instead: S///
given $foo

Hence the code above. If you like this approach, then I encourage you to
look at Raku's "ordinal" (i.e. positional) regex adverb(s): `:1st`, `:2nd`,
`:3rd`, etc. See:

https://docs.raku.org/language/regexes#Positional_adverbs

OTOH, if you **really** have elements separated by a fixed character (e.g.
"\n" or "\t"), you can `.split()` on that element, and use the positional
information to extract the (sub) elements you desire:

$ 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] > my $tsv = "this\tis\ta\ttest";
this is a test
[1] > say $tsv.elems;
1
[1] > say $tsv.split("\t").raku;
("this", "is", "a", "test").Seq
[1] > say $tsv.split("\t").elems;
4
[1] > say $tsv.split("\t")[3];
test
[1] > say $tsv.split("\t").grep(/ t /, :p).raku;
(0 => "this", 3 => "test").Seq
[1] > say $tsv.split("\t").grep(/^ t /, :p).raku;
(0 => "this", 3 => "test").Seq
[1] > say $tsv.split("\t").grep(/ s /, :p).raku;
(0 => "this", 1 => "is", 3 => "test").Seq
[1] > say $tsv.split("\t").grep(/^ s /, :p).raku;
().Seq

Please note: for the `grep` examples above, `grep` isn't necessarily
returning the 1st, 2nd, or 3rd letter  "t" in the original string, since
each returned (matching) element could contain multiple "t" characters. So
you really have to decide what you want to target: the n-th "t" in a
string, or the n-th **tab-separated word** containing a "t" in the string.

HTH, Bill.

Reply via email to