Need help with a regex

Hi All,

What am I doing wrong here?

$ p6 'my $x=3D"\$1.23"; $x~~s/("\$") (.*?)/$1USD/; say $x;'
USD1.23

I am expecting to see=C2=A0 `1.23USD`

Many thanks,
-T






--=20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
perl6
5/7/2019 3:09:43 AM
perl.perl6.users 1192 articles. 0 followers. Follow

3 Replies
36 Views

Similar Articles

[PageSpeed] 45

--000000000000933c4b058843c3b1
Content-Type: text/plain; charset="UTF-8"

The .*? expression is not matching anything. I think you want to use .+

perl6 -e 'my $x="\$1.23"; $x~~s/("\$") (.*?)/$0:$1:USD/; say $x;'
$::USD1.23

perl6 -e 'my $x="\$1.23"; $x~~s/ \$(.+) /$0USD/; say $x'
1.23USD



On Tue, 7 May 2019 at 13:10, ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> What am I doing wrong here?
>
> $ p6 'my $x="\$1.23"; $x~~s/("\$") (.*?)/$1USD/; say $x;'
> USD1.23
>
> I am expecting to see  `1.23USD`
>
> Many thanks,
> -T
>
>
>
>
>
>
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Computers are like air conditioners.
> They malfunction when you open windows
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>


-- 
Norman Gaywood, Computer Systems Officer
School of Science and Technology
University of New England
Armidale NSW 2351, Australia

ngaywood@une.edu.au  http://turing.une.edu.au/~ngaywood
Phone: +61 (0)2 6773 2412  Mobile: +61 (0)4 7862 0062

Please avoid sending me Word or Power Point attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html

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

<div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr">The .*? expression is no=
t matching anything. I think you want to use .+</div><div dir=3D"ltr"><br><=
div><div>perl6 -e &#39;my $x=3D&quot;\$1.23&quot;; $x~~s/(&quot;\$&quot;) (=
..*?)/$0:$1:USD/; say $x;&#39;</div><div>$::USD1.23</div></div><div><br></di=
v><div><div>perl6 -e &#39;my $x=3D&quot;\$1.23&quot;; $x~~s/ \$(.+) /$0USD/=
; say $x&#39;</div><div>1.23USD</div></div><div><br></div><div><br></div></=
div></div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gm=
ail_attr">On Tue, 7 May 2019 at 13:10, ToddAndMargo via perl6-users &lt;<a =
href=3D"mailto:perl6-users@perl.org">perl6-users@perl.org</a>&gt; wrote:<br=
></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;=
border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi All,<br>
<br>
What am I doing wrong here?<br>
<br>
$ p6 &#39;my $x=3D&quot;\$1.23&quot;; $x~~s/(&quot;\$&quot;) (.*?)/$1USD/; =
say $x;&#39;<br>
USD1.23<br>
<br>
I am expecting to see=C2=A0 `1.23USD`<br>
<br>
Many thanks,<br>
-T<br>
<br>
<br>
<br>
<br>
<br>
<br>
-- <br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
Computers are like air conditioners.<br>
They malfunction when you open windows<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
</blockquote></div><br clear=3D"all"><div><br></div>-- <br><div dir=3D"ltr"=
 class=3D"gmail_signature"><div dir=3D"ltr"><div>Norman Gaywood, Computer S=
ystems Officer</div><div>School of Science and Technology<br>University of =
New England</div><div>Armidale=C2=A0NSW 2351, Australia<br><br><a href=3D"m=
ailto:ngaywood@une.edu.au" target=3D"_blank">ngaywood@une.edu.au</a>=C2=A0=
=C2=A0<a href=3D"http://turing.une.edu.au/~ngaywood" target=3D"_blank">http=
://turing.une.edu.au/~ngaywood</a></div><div>Phone: +61 (0)2 6773 2412 =C2=
=A0Mobile: +61 (0)4 7862 0062<br><br>Please avoid sending me Word or Power =
Point attachments.<br>See <a href=3D"http://www.gnu.org/philosophy/no-word-=
attachments.html" target=3D"_blank">http://www.gnu.org/philosophy/no-word-a=
ttachments.html</a><br></div></div></div>

--000000000000933c4b058843c3b1--
0
ngaywood
5/7/2019 3:24:57 AM
>> 
>> On Tue, 7 May 2019 at 13:10, ToddAndMargo via perl6-users 
>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>> 
>>     Hi All,
>> 
>>     What am I doing wrong here?
>> 
>>     $ p6 'my $x="\$1.23"; $x~~s/("\$") (.*?)/$1USD/; say $x;'
>>     USD1.23
>> 
>>     I am expecting to see  `1.23USD`
>> 
>>     Many thanks,
>>     -T

On 5/6/19 8:24 PM, Norman Gaywood wrote:
> The .*? expression is not matching anything. I think you want to use .+
> 
> perl6 -e 'my $x="\$1.23"; $x~~s/("\$") (.*?)/$0:$1:USD/; say $x;'
> $::USD1.23
> 
> perl6 -e 'my $x="\$1.23"; $x~~s/ \$(.+) /$0USD/; say $x'
> 1.23USD

Hi Normin,

That was it.  I wasn't greedy enough.

Removing the ?


$ p6 'my $x="\$1.23"; $x~~s/("\$") (.*)/$1 USD/; say $x;'
1.23 USD


Thank you!
0
perl6
5/7/2019 4:09:17 AM
>> On Tue, 7 May 2019 at 13:10, ToddAndMargo via perl6-users=20
>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>>=20
>>     Hi All,
>>=20
>>     What am I doing wrong here?
>>=20
>>     $ p6 'my $x=3D"\$1.23"; $x~~s/("\$") (.*?)/$1USD/; say $x;'
>>     USD1.23
>>=20
>>     I am expecting to see  `1.23USD`
>>=20
>>     Many thanks,
>>     -T
>>=20

On 5/6/19 8:28 PM, Kevin Pye wrote:
> ".*?" doesn't mean what you think it does. ".*" means basically=20
> anything, without the "?". Adding the "?" changes the meaning from=20
> "match as much as possible" to "match as little as possible" (i.e.=20
> frugal matching rather than eager matching).
>=20
> Thus ".*?" matches nothing since there's nothing after it to force it t=
o=20
> match something. The dollar sign and the empty string following it (the=
=20
> ".*?") are replaced with "USD" and the remaining number is=C2=A0left al=
one.
> There's also no need for the parentheses around the escaped dollar sign=
=2E=20
> "s/\$(.*)/$0USD/" should work fine, although I'd probably use=20
> "s/\$(.*)/{$0}USD/" to make things a little clearer.
>=20
> See=20
> https://docs.perl6.org/language/regexes#Greedy_versus_frugal_quantifier=
s:_?
>=20
> Kevin.
>=20

Hi Kevin,

That was a really great explanation.  Thank you!

As it turned out, it was a screw up on my part.   I forget the
? in .*? so often that I have started typing it automatically.

I also frequently type "my" before all my variables and am slowly
braking that habit.  One "my" per customer.

And to add to the fray, I use mixed case variables ($NewStr)
exclusively.  I know how to type and it cost me no time and
it differentiates my variables for others variables.  So
consequently, I type "my" as "My" about 50% of the time.

:-)

-T
0
perl6
5/7/2019 6:04:05 AM
Reply: