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

Re: How do I use chr inside a regex?

Thread Previous
From:
ToddAndMargo via perl6-users
Date:
February 2, 2019 09:00
Subject:
Re: How do I use chr inside a regex?
Message ID:
3ad74f6b-2120-7f86-d126-96421aaf72a7@zoho.com
On 2/1/19 11:32 PM, JJ Merelo wrote:
> Hi,
> 
> El sáb., 2 feb. 2019 a las 7:48, ToddAndMargo via perl6-users 
> (<perl6-users@perl.org <mailto: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

Thank you!

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