perlynewby has asked for the wisdom of the Perl Monks concerning the following question:
I have read and tested my code but I don't quite understand the output in 2 cases, will you explain?
I get two different output here from a single regex expression and the difference is a space between what I assigned to be my $1 and my $2 captures
I have googled this and read thru perl tutorials and I have found no real explanation that can answer my simple question, perhaps you can help?
s/^(.*)+([^ ]+)$/$2, $1/; #$1 is anything that starts is 1st name so / +^(.*) and $2=anything else after
this worked, please note the space between $1 and when I try to capture the $2 value...I only found this worked because I fat fingered it while looking for the reason my first try didn't work..,I ended up doing it differently to capture right last name but I still have need to get answer on this
s/^(.*) +([^ ]+)$/$2, $1/;
entire test code that gives me an error for demo
use strict; my @cognome = last_name_first("leonardo Da Vincy","Raffaello da Urbino +"); print join(";",@cognome); sub last_name_first { my@names=@_; foreach(@names){ s/^(.*)+([^ ]+)$/$2, $1/; #$1 anything that starts is 1st name a +nd $2=anything else after..FAIL! #s/^(.*) +([^ ]+)$/$2, $1/; #works... space seems to do it,why? } return @names; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: need help with explaining the output
by Athanasius (Archbishop) on Jul 16, 2015 at 07:24 UTC | |
by perlynewby (Scribe) on Jul 20, 2015 at 22:03 UTC | |
|
Re: need help with explaining the output
by 1nickt (Canon) on Jul 16, 2015 at 00:04 UTC | |
by perlynewby (Scribe) on Jul 16, 2015 at 00:26 UTC | |
by stevieb (Canon) on Jul 16, 2015 at 00:36 UTC | |
by u65 (Chaplain) on Jul 16, 2015 at 11:32 UTC | |
|
Re: need help with explaining the output
by stevieb (Canon) on Jul 16, 2015 at 00:23 UTC | |
by perlynewby (Scribe) on Jul 16, 2015 at 00:33 UTC | |
|
Re: need help with explaining the output -- regex tools
by Discipulus (Canon) on Jul 16, 2015 at 07:43 UTC | |
by perlynewby (Scribe) on Jul 22, 2015 at 23:12 UTC |