Hi monks,
I'm hoping someone can help me/teach me about regex's. I can do basic ones but i'm trying to do something and it's got me quite stumped.
Best way to describe it is with an example so here we go:
#!/usr/bin/perl
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" );
# Results should be:
# Smith J = Smith, John
# Jackson J = Jackson J
# James RJ = James, Ray Jack
# Van der Burg J = Van der Burg, Jon
# O'Neil S = O'Neil, Sarah
foreach my $author (@authors) {
print "$author = ";
# Regex rules
# Last ' ' before all-uppercase word should become ', '
# Every singular or grouped capital letter
# (i.e. F or RJ) should become F(.*) or R(.*) J(.*)
# What I have so far
$author =~ s/ (\w+?)\p{IsUpper}/, $1\(\.*\)/;
print "[ $author ] : ";
if ( my ($match) = ( grep $_ =~ /$author/, @full_authors ) ) {
$author = $match;
@full_authors = grep { $_ ne $match } @full_authors;
} # end-if
print "$author\n";
} # end-foreach
exit;
Any help anyone could provide would be much appreciated.
Cheers,
Reagen
In reply to regex help
by rsiedl
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.