#!/usr/bin/perl -l use strict; use warnings; my @full_authors = ( "Smith, John", "Smith, John Ronald", "Johnson, James", "James, Ray Jack", "Van der Burg, Jon", "O'Neil, Sarah" ); my @authors = ( "Smith J", "Jackson J", "James RJ", "Van der Burg J", "O'Neil S" ); $, = " & "; foreach my $a (@authors) { my ($f, $s) = $a =~ m/(.*)\s+(\w+)$/; # This regex will put everythin before the last space in $f, and everything after the space in $s # Example: if $a is James RJ, then $f is 'James', and $s 'RJ' my @g = split //, $s; # Split the last charachter, so that each element in the @g-array is one letter $f = quotemeta($f); # Remove all special thingies of $f, like a . and a space (needed because the /x modifier is in use) my $p = join('\w+\s+', @g); # Join the letters together, and add the regex charachter \w+\s+, meaning that RJ will become R\w+\s+J (which is used in the regex) my @x = grep (m/ ^ # Match start of line $f # Match the last name \s* # Match some optional whitespace , # Match a comma \s+ # Match some whitespace (not optinal) $p # Match the second part of the name \w+ # Match the remaining word-charachters of this name \s* # Match some optional whitespace $ # Match the end /xi, @full_authors); print $a, @x; }