简体   繁体   English

为什么此正则表达式不能在我的Python中运行?

[英]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. 我正在尝试编写一个区分姓,名和中间名的Python程序。 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. 问题在于姓氏是中间名,我以为在末尾使用大写字母S可以使它姓,所以它只选择在单词前加空格的单词来查找姓,但lastNameRegex是只是拿起中间名。

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. 此外,该代码还应采用“约翰·约瑟夫·史密斯”之类的名称,并分隔每个名称,因此“ \\ w +”代表姓氏,“ \\ s \\ w * \\ s”代表姓氏。

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')

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. 我还使用了middleNameRegex中的middleNameRegex 和lookahead ,以便结果名称中不会包含空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM