in reply to regex for string

It's tough to do this for edge cases in one regex, so I would solve it in two. First, grab the first character:

$str =~ m/\A(.)/xms; my $first_character = $1;

Then, grab the last character:

$str =~ m/(.)\z/xms; my $last_character = $1;

If you could guarantee the string was at least two characters long, you could do it in one regex:

$str =~ m/\A(.).*(.)\z/xms; my $first_character = $1; my $last_character = $2;

If you insist on handling all edge cases in one regex, here's one way:

$str =~ m/\A(.).*?(.?)\z/xms; my $first_character = $1; my $last_character = $2; $last_character = $first_character if ! $last_character;