Need help with a regex #2

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
0
perl6
5/7/2019 2:12:39 AM
perl.perl6.users 1183 articles. 0 followers. Follow

2 Replies
5 Views

Similar Articles

[PageSpeed] 19

The (.*?) pattern will match an empty string. =20

Thus $0 gets the dollar sign, $1 is "", and "$" ~ "" (i.e., "$") gets rep=
laced by "" ~ "USD"  (i.e., "USD").

So the net result is to replace the single dollar sign by "USD", resultin=
g in "USD1.23".

You might want to remove the ? modifier from .*?, so that the expresssion=
 is greedy instead of eager.

Pm


On Mon, May 06, 2019 at 07:12:39PM -0700, Tony Ewell via perl6-users wrot=
e:
> 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=A0 `1.23USD`
>=20
> Many thanks,
> -T
0
pmichaud
5/7/2019 10:05:06 PM
 > On Mon, May 06, 2019 at 07:12:39PM -0700, Tony Ewell via perl6-users 
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/7/19 3:05 PM, Patrick R. Michaud wrote:
> The (.*?) pattern will match an empty string.
> 
> Thus $0 gets the dollar sign, $1 is "", and "$" ~ "" (i.e., "$") gets replaced by "" ~ "USD"  (i.e., "USD").
> 
> So the net result is to replace the single dollar sign by "USD", resulting in "USD1.23".
> 
> You might want to remove the ? modifier from .*?, so that the expresssion is greedy instead of eager.
> 
> Pm

I wasn't "greedy" enough.  Chuckle!

Thank you!
0
perl6
5/8/2019 12:18:00 AM
Reply: