in reply to RegExp, grabbing first name

You might be looking for $+. Quoting the docs:

The text matched by the last bracket of the last successful search pattern. This is useful if you don't know which one of a set of alternative patterns matched. For example:

/Version: (.*)|Revision: (.*)/ && ($rev = $+);

Replies are listed 'Best First'.
Re^2: RegExp, grabbing first name
by grasshopper!!! (Beadle) on Feb 25, 2016 at 20:55 UTC

    Try this see if it will work.

    #!/usr/bin/perl use strict; use warnings; #my $mystring = "SMITH, A DOE"; my $mystring = "BULLOCK JOE A"; my @array= $mystring =~ m/^[A-Z, ]+([A-Z]{3,}).*?$/; print"$array[0]\n";
      Could this one work?

      if ($string =~ /(\w{3,})$/ ) { print "\n\$1\n"; }elsif( $string =~ /(\w+)\s(\w{1,2})$/ ) { #}elsif( $string =~ /(\w{1})\s(\w+)\s(\w{1,2})$/) { # or this one usin +g $2 print "\n$1\n"; }
      It doesn't work:
      my $string = "BULLOCK MICHAEL A"; $string = ($string =~ m/^[A-Z, ]+([A-Z]{3,}).*?$/) ? $1 : ''; print AEL;