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; }

In reply to need help with explaining the output by perlynewby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.