How do I use chr inside a regex?

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;'

xy


Many thanks,
-T
0
perl6
2/2/2019 6:32:42 AM
perl.perl6.users 1168 articles. 0 followers. Follow

2 Replies
39 Views

Similar Articles

[PageSpeed] 19

--0000000000009743c00580e443bf
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi,

El s=C3=A1b., 2 feb. 2019 a las 7:48, ToddAndMargo via perl6-users (<
perl6-users@perl.org>) escribi=C3=B3:

> Hi All,
>
> How do I use chr inside a regex.  In the below, how
> do I get rid of $y?
>
> $ p6 'my Str $x=3Dchr(0x66)~chr(0x77); my Str $y=3Dchr(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 =3D "\x66\x77"; $x ~~ s/\x66\x77/xy/; say $x # OUTPUT: =C2=ABxy=E2=90=
=A4=C2=BB

(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 =3D "\x66\x77"; $x ~~ s/$(chr(0x66)~chr(0x77))/xy/; say $x; # OUTPUT:
=C2=ABxy=E2=90=A4=C2=BB

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

Cheers

JJ

--0000000000009743c00580e443bf
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div di=
r=3D"ltr"><div>Hi,<br></div><br><div class=3D"gmail_quote"><div dir=3D"ltr"=
 class=3D"gmail_attr">El s=C3=A1b., 2 feb. 2019 a las 7:48, ToddAndMargo vi=
a perl6-users (&lt;<a href=3D"mailto:perl6-users@perl.org">perl6-users@perl=
..org</a>&gt;) escribi=C3=B3:<br></div><blockquote class=3D"gmail_quote" sty=
le=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);paddi=
ng-left:1ex">Hi All,<br>
<br>
How do I use chr inside a regex.=C2=A0 In the below, how<br>
do I get rid of $y?<br>
<br>
$ p6 &#39;my Str $x=3Dchr(0x66)~chr(0x77); my Str $y=3Dchr(0x66)~chr(0x77);=
 <br>
$x~~s/ $y /xy/; say $x;&#39;<br>
<br></blockquote><div>If what you want to do is precisely what you are doin=
g, you don&#39;t even need to use chr:</div><div><br></div></div>my $x =3D =
&quot;\x66\x77&quot;; $x ~~ s/\x66\x77/xy/; say $x # OUTPUT: =C2=ABxy=E2=90=
=A4=C2=BB=C2=A0 <br></div><div dir=3D"ltr"><br></div><div>(See the document=
 on quoting: <a href=3D"https://docs.perl6.org/language/quoting#Interpolati=
on:_qq">https://docs.perl6.org/language/quoting#Interpolation:_qq</a>) <br>=
</div><div><br></div><div>However, if what you want to do is what you _say_=
 you are doing,=C2=A0</div><div><br></div><div>my $x =3D &quot;\x66\x77&quo=
t;; $x ~~ s/$(chr(0x66)~chr(0x77))/xy/; say $x; # OUTPUT: =C2=ABxy=E2=90=A4=
=C2=BB=C2=A0 <br></div><div><br></div><div>$() interpolates within a regex,=
 as indicated in the documentation: <a href=3D"https://docs.perl6.org/langu=
age/regexes#index-entry-regex__Regex_Interpolation-Regex_interpolation">htt=
ps://docs.perl6.org/language/regexes#index-entry-regex__Regex_Interpolation=
-Regex_interpolation</a></div><div><br></div><div>Cheers</div><div><br></di=
v><div>JJ<br></div></div></div></div></div>

--0000000000009743c00580e443bf--
0
jjmerelo
2/2/2019 7:32:17 AM
On 2/1/19 11:32 PM, JJ Merelo wrote:
> Hi,
>=20
> El s=C3=A1b., 2 feb. 2019 a las 7:48, ToddAndMargo via perl6-users=20
> (<perl6-users@perl.org <mailto:perl6-users@perl.org>>) escribi=C3=B3:
>=20
>     Hi All,
>=20
>     How do I use chr inside a regex.=C2=A0 In the below, how
>     do I get rid of $y?
>=20
>     $ p6 'my Str $x=3Dchr(0x66)~chr(0x77); my Str $y=3Dchr(0x66)~chr(0x=
77);
>     $x~~s/ $y /xy/; say $x;'
>=20
> If what you want to do is precisely what you are doing, you don't even =

> need to use chr:
>=20
> my $x =3D "\x66\x77"; $x ~~ s/\x66\x77/xy/; say $x # OUTPUT: =C2=ABxy=E2=
=90=A4=C2=BB
>=20
> (See the document on quoting:=20
> https://docs.perl6.org/language/quoting#Interpolation:_qq)
>=20
> However, if what you want to do is what you _say_ you are doing,
>=20
> my $x =3D "\x66\x77"; $x ~~ s/$(chr(0x66)~chr(0x77))/xy/; say $x; #=20
> OUTPUT: =C2=ABxy=E2=90=A4=C2=BB
>=20
> $() interpolates within a regex, as indicated in the documentation:=20
> https://docs.perl6.org/language/regexes#index-entry-regex__Regex_Interp=
olation-Regex_interpolation
>=20
> Cheers
>=20
> JJ

Thank you!
0
perl6
2/2/2019 9:00:10 AM
Reply: