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

Re: how do I do a literal string in the target of a regex?

Thread Previous
From:
ToddAndMargo via perl6-users
Date:
October 30, 2022 00:53
Subject:
Re: how do I do a literal string in the target of a regex?
Message ID:
c64fcf62-48e6-d0fc-c337-1a396610909f@zoho.com

>> On 29 Oct 2022, at 23:28, ToddAndMargo via perl6-users <perl6-users@perl.org> wrote:
>>
>>
>>>> On 29 Oct 2022, at 22:02, ToddAndMargo via perl6-users <perl6-users@perl.org> wrote:
>>>>
>>>> Hi All,
>>>>
>>>> I am trying to change
>>>>
>>>>      /
>>>>
>>>> into
>>>>
>>>>      \\\
>>>>
>>>> This works:
>>>>
>>>>
>>>> $ echo "a/b/c/d" | raku -ne 'my $x=$_; $x~~s:g|$(Q[/])|\\\\\\|;print $x ~ "\n"'
>>>> a\\\b\\\c\\\d
>>>>
>>>>
>>>> But this does not:
>>>>
>>>> $ echo "a/b/c/d" | raku -ne 'my $x=$_; $x~~s:g|$(Q[/])|Q[\\\]|;print $x ~ "\n"'
>>>> aQ[\]bQ[\]cQ[\]d
>>>>
>>>>
>>>> How do I put a literal string in the
>>>> target of a regex?
>>>>
>>>> Many thanks,
>>>> -T
>>
>> On 10/29/22 13:07, Elizabeth Mattijsen wrote:
>>> $ echo "a/b/c/d" | raku -ne 'say .subst("/", Q/\\\/, :g)'
>>>
>>
>> Hi Elizabeth,
>>
>> Thank you for the subst workaround!
>>
>> Do you also know how to do a literal string in
>> the target of the regex?
>>
>> -T

On 10/29/22 14:48, Elizabeth Mattijsen wrote:
 > / 'literal string in target' /
 >

I am not following.  ;-(

[0] > my $x="a/b/c/d"; $x~~s:g|$(Q[/])|'\\\'|; print $x ~ "\n"
a'\'b'\'c'\'d

[0] > my $x="a/b/c/d"; $x~~s:g|$(Q[/])|/\\\/|; print $x ~ "\n"
a/\/b/\/c/\/d

[0] > my $x="a/b/c/d"; $x~~s:g|$(Q[/])|/'\\\/'|; print $x ~ "\n"
a/'\/'b/'\/'c/'\/'d

[0] > my $x="a/b/c/d"; $x~~s:g|$(Q[/])|/'\\\'/|; print $x ~ "\n"
a/'\'/b/'\'/c/'\'/d

[0] > my $x="a/b/c/d"; $x~~s:g|$(Q[/])|\\\|; print $x ~ "\n"
===SORRY!=== Error while compiling:
Malformed replacement part; couldn't find final |
at line 2
------> <BOL>⏏<EOL>
     expecting any of:
         postfix






Thread Previous


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