Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: regex: seperating parts of non-formatted names

by Django (Pilgrim)
on Sep 09, 2002 at 19:17 UTC ( [id://196403]=note: print w/replies, xml ) Need Help??


in reply to regex: seperating parts of non-formatted names

Breaking the thing into pieces is surely the way to go. My following code works with the included test names, but there may be some names that break it. The fields will contain trailing spaces, which can be removed afterwards.

#!usr/bin/perl -w @Data = ( 'Dr. Foo B. Baz', 'Ms Bar', 'Foo Bar', 'Col Foo Bar', 'Foo E.G. Bar', 'Baz', ) ; my $Title = qr/ (?: LTC | COL | DR | MS | MR | MISS ) /ix ; for (@Data) { / ( (?: $Title \.? \s+ )? ) ( (?: [\w-]+ \s+ )? ) ( (?: (?: \w\.\s*? )+ \s+ )? ) ( [\w-]+? \s* $ ) /ix; my $i++; ( $Fields{'title' }[$i], $Fields{'name' }[$i], $Fields{'initials'}[$i], $Fields{'surname' }[$i], ) = ( $1, $2, $3, $4 ); print "$1:$2:$3:$4\n"; } __DATA__ Dr. :Foo :B. :Baz Ms :::Bar :Foo ::Bar Col :Foo ::Bar :Foo :E.G. :Bar :::Baz

~Django
"Why don't we ever challenge the spherical earth theory?"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://196403]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-23 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found