in reply to Extract firstname and lastname from variable

The problem with your regexes is that you use the start of string anchor ^ in the middle of the string where it can't possibly match. In fact split is likely to be a better tool for this exercise. Consider:

use strict; use warnings; for my $name (qw'JoeZZZJones JoeZZZJosephZZZJones Bobbie') { my @parts = split 'ZZZ', $name; print "Name: $name\n"; print "Name part $_: $parts[$_ - 1]\n" for 1 .. @parts; print "\n"; }

Prints:

Name: JoeZZZJones Name part 1: Joe Name part 2: Jones Name: JoeZZZJosephZZZJones Name part 1: Joe Name part 2: Joseph Name part 3: Jones Name: Bobbie Name part 1: Bobbie

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Extract firstname and lastname from variable
by colwellj (Monk) on Mar 01, 2010 at 02:41 UTC
    split also has the advantage that if you hit someone with a middle name you'll get three items in your array.
Re^2: Extract firstname and lastname from variable
by llancet (Friar) on Mar 01, 2010 at 03:04 UTC
    This one absolutely good:
    /^([A-Z][a-z]+)([A-Z]+)?([A-Z][a-z]+)?$/ # stored in $1, $2, $3
    it works with:
    FooBBBBar FooBar Foo