Re: I need regex last instance help
[email protected] (ToddAndMargo via perl6-users) Sat, 25 Oct 2025 16:09:16 -0700
| Newsgroups | perl.perl6.users |
|---|---|
| Message-ID | <[email protected]> |
>> On Oct 25, 2025, at 05:19, ToddAndMargo via perl6-users <perl6- >> [email protected]> wrote: >> >> Hi All, >> >> What am I doing wrong here: >> >> [2] > my Str $x = "1.40.564"; $x~~s:i/ '.' $$ .* //; print "$x\n"; >> 1.40.564 >> >> I am after "1.40" >> >> This is my work around, but I'd really like to know >> how to do it right: >> >> my Str $x = "1.40.564"; $x~~m:i/ ( .*? '.') (.*? ) '.' /; print "$0$1\n"; >> 1.40 >> >> Your in confusion, >> -T > On 10/25/25 8:06 AM, William Michels wrote: > [0] > my $x = "1.40.564"; $x = $x ~~ m/ ^ <-[.]>+ "." <-[.]>+ /; say ~$x; > 1.40 > [0] > my $x = "1.40.564"; $x = $x.match(/ ^ <-[.]>+ "." <-[.]>+ /); say > ~$x; > 1.40 > > #OR > > [1] > my $x = "1.40.564"; $x = S/ "." <-[.]>+ $ // given $x; say $x; > 1.40 > [1] > my $x = "1.40.564"; $x = $x.subst(/ "." <-[.]>+ $ /, "" ); say $x; > 1.40 > > One issue with your code was failing to create a `<-[.]>` enumerated > character class. > This (negated) `<-[.]>` class consists of all characters other than `.` > dot (i.e. period). > > Now read the regexes above: > 1. The first two matching expressions look for "start-of-string, > non-dot characters, a dot, then more non-dot characters". > 2. The last two substitution expressions look for "a dot, non-dot > characters, then end-of-string". > > HTH, Bill. > Hi Bill, Thank you. It does! It also tells me that the easiest way way actually the "m' work around I came up with and that I have no understanding of $$ and ^^ in regexes. So I have some work to do. -T