Kayden K
Kayden K

Reputation: 29

Why isn't this regex working in my Python?

I am attempting to write a Python program that distinguishes between first, last, and middle names. I am using regular expressions to do it, the problematic code is below.

The issue is that the last name is picking up the middle name, I thought using a capital S at the end would make it so it only picks the word with a space before but not after it, to find the last name but the lastNameRegex is just picking up the middle name.

Also, the code is meant to take a name like 'John Joseph Smith' and separate each name, hence the '\w+' for first name and '\s\w*\s' for last name.

Thanks for all help, and I'm pretty new to all this stuff so all constructive criticism is welcome. Thanks! :)

firstNameRegex = re.compile(r'\w+')
middleNameRegex = re.compile(r'\s\w*\s')
lastNameRegex = re.compile(r'\s\w+\S')

Upvotes: 0

Views: 146

Answers (1)

Barmar
Barmar

Reputation: 780798

You should anchor the regular expressions if you want them to match only at specific places in the string. ^ matches the beginning of the string, $ matches the end.

firstNameRegex = re.compile(r'^\w+')
middleNameRegex = re.compile(r'(?<=\s)\w*(?=\s)')
lastNameRegex = re.compile(r'\w+$')

I've also used lookbehind and lookahead in middleNameRegex so that the spaces won't be included in the result, just the name itself.

Upvotes: 1

Related Questions