0

I have a regex expression in Perl that I've been trying to figure out for a while, but still have trouble understanding what it does. The regex is the following:

($last,$first,$middle) = $name =~ /^(.*)_([A-Z][^A-Z]*)([A-Z][a-z]*)?/;

The (.*) means any sequence of characters, _ followed by underscore, not sure what ([A-Z][^A-Z]*) and ([A-Z][a-z]*)? do.

And also how is expression executed? Is this same as below:

$name =~ /^(.*)_([A-Z][^A-Z]*)([A-Z][a-z]*)?/;
($last,$first,$middle) = $name

Thanks for any help!

LifeAndHope
  • 594
  • 1
  • 7
  • 24
  • 1
    [`perldoc perlre`](http://perldoc.perl.org/perlre.html), [`perldoc perlretut`](http://perldoc.perl.org/perlretut.html). – Biffen Jan 08 '15 at 15:23
  • 1
    characters inside the 1st capturing group goes to the variable `last`, second goes to `first` and the optional third goes to `middle` – Avinash Raj Jan 08 '15 at 15:25
  • 1
    As for the second part: No. Think of the `=~` operation as returning a list. It would be more like `$name =~ /^(.*)_([A-Z][^A-Z]*)([A-Z][a-z]*)?/; $last = $1; $first = $2; $middle = $3;`. Also, parsing peoples' names like that might be [a bad idea](http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/). – Biffen Jan 08 '15 at 15:25
  • 1
    There are various tools online that can help you understand/visualise regex patterns, for example: https://www.debuggex.com/r/NinulRIdvbrpu04w – Tom Lord Jan 08 '15 at 17:09
  • Thanks for the suggestions. I really like the debuggex! – LifeAndHope Jan 09 '15 at 22:01

0 Answers0