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:28 PM, Kevin Pye wrote:
".*?" doesn't mean what you think it does. ".*" means basically anything, without the "?". Adding the "?" changes the meaning from "match as much as possible" to "match as little as possible" (i.e. frugal matching rather than eager matching).

Thus ".*?" matches nothing since there's nothing after it to force it to match something. The dollar sign and the empty string following it (the ".*?") are replaced with "USD" and the remaining number is left alone. There's also no need for the parentheses around the escaped dollar sign. "s/\$(.*)/$0USD/" should work fine, although I'd probably use "s/\$(.*)/{$0}USD/" to make things a little clearer.

See https://docs.perl6.org/language/regexes#Greedy_versus_frugal_quantifiers:_?

Kevin.


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

Reply via email to