You can use grep.

Some working code: (all you need to do to let this suit your needs is check if @x has elements)

#!/usr/bin/perl -l use strict; use warnings; my @full_authors = ( "Smith, John", "Smith, John Ronald", "Johnson, Ja +mes", "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 everythi +n 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 eac +h element in the @g-array is one letter $f = quotemeta($f); # Remove all special thingies of $f, like a . an +d 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+, me +aning 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; }

If you have 'Smith, John' and 'Smith, Jack' in your @full_authors-array and you are searching for 'Smith J' then @x-array will have both these elemnts.

Update, added the note about duplicates


In reply to Re: regex help by Animator
in thread regex help by rsiedl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.