develooper Front page | perl.perl6.users | Postings from February 2019

Re: How do I use chr inside a regex?

Thread Previous | Thread Next
From:
JJ Merelo
Date:
February 2, 2019 07:32
Subject:
Re: How do I use chr inside a regex?
Message ID:
CAJoLtgcjFE0JSon0xAXk67nNkOQstVHZrVQP-B0ow+6pT3xcxw@mail.gmail.com
Hi,

El sáb., 2 feb. 2019 a las 7:48, ToddAndMargo via perl6-users (<
perl6-users@perl.org>) escribió:

> Hi All,
>
> How do I use chr inside a regex.  In the below, how
> do I get rid of $y?
>
> $ p6 'my Str $x=chr(0x66)~chr(0x77); my Str $y=chr(0x66)~chr(0x77);
> $x~~s/ $y /xy/; say $x;'
>
> If what you want to do is precisely what you are doing, you don't even
need to use chr:

my $x = "\x66\x77"; $x ~~ s/\x66\x77/xy/; say $x # OUTPUT: «xy␤»

(See the document on quoting:
https://docs.perl6.org/language/quoting#Interpolation:_qq)

However, if what you want to do is what you _say_ you are doing,

my $x = "\x66\x77"; $x ~~ s/$(chr(0x66)~chr(0x77))/xy/; say $x; # OUTPUT:
«xy␤»

$() interpolates within a regex, as indicated in the documentation:
https://docs.perl6.org/language/regexes#index-entry-regex__Regex_Interpolation-Regex_interpolation

Cheers

JJ

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