Re: Need help with a regex
> 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!
Re: Need help with a regex
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 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
Need help with a regex
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
Re: Need help with a regex
On Tue, 7 May 2019 at 13:10, ToddAndMargo via perl6-users 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
Re: Need help with a regex
On Tue, 7 May 2019 at 13:10, ToddAndMargo via perl6-users 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!
Re: Need help with a regex
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 ngayw...@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
Need help with a regex
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 ~~