Reputation: 109
I have some records of people that I want to breakdown the information for in the following way:
Possibly a few incorrect assumptions in here, i.e. single word surnames. I'm looking to use regex to do the bulk and am keep to get ideas around this. The implementation of the regular expression will be in PHP.
Here are some example records:
Sam Leicester
Sam Christopher Leicester
Sam Leicester (London)
Sam Christopher Leicester (France)
So far I've produced this http://regexr.com/39cbk which parses the names into an array (easy to use last element as surname, and rest joined as firstname(s)) With a recond group for the location. Although I don't seem to have any joy implementing this with preg_match() yet.
Upvotes: 1
Views: 133
Reputation: 401
You can use named groups to make life easier and use such regexp:
/^(?P<name>\w+ (?:\w+ )*?)(?P<secondName>\w+(?: |$))?(?:\((?P<location>\w+)\))?$/m
Or without named groups:
/^(\w+ (?:\w+ )*?)(\w+(?: |$))?(?:\((\w+)\))?$/m
This regexp works well also when user specified only name and location.
Upvotes: 3
Reputation: 338
You can use this regex:
/^(.*)\s(\w+)(?:\s\((\w+)\))?$/
The first capturing group (.*)
attempts to match anything. This allows the engine to backtrack. If locations in brackets are absent, we simply match the surname ((\w+)
) and escape, otherwise we will match both the surname (\w+)
and the location in two Capturing groups.
Upvotes: 5