On Tue, May 1, 2018 at 8:37 AM, ToddAndMargo <toddandma...@zoho.com> wrote:

> Hi All,
>
> I am trying to change the last three letters of a string
>
> $ perl6 -e 'my $x="abcabcabc"; $x ~~ s/"a.*"$/xyz/; say $x;'
>

The double quotes around your text make it a string literal, so it will
only match the literal string "a.*" at the end of the string.

➤ perl6 -e 'my $x="abcabca.*"; $x ~~ s/"a.*"$/xyz/; say $x;'
abcabcxyz

Another way you could accomplish your goal (other than the ones already
mentioned elsewhere) is to use (.*) at the front of the regex to greedily
match and capture as much as possible until the "a", then match the rest of
the string and replace it with what you matched plus "xyz" ...

➤ perl6 -e 'my $x="abcabcabc"; $x ~~ s/(.*)a.*$/$0xyz/; say $x;'
abcabcxyz

Though, this could copy a significant amount of text depending on the part
of the string before that final "a".  Also, there could be significant
backtracking depending on the part of the string after that final "a".
But, sometimes it's a useful technique, so I mention it.

cheers,

-Scott




> abcabcabc
>
> I want abcabcxyz
>
> And, in real life, only the "a" will be a know letter.
> Everything else will vary.  And the "a" will repeat a lot.
> I am only interested in changing the last "a" and everything
> that comes after it.
>
> Many thanks,
> -T
>

Reply via email to