python获取英文 firstname 和 lastname 正则匹配 练习记录

import re

import pyperclip


def getName(text):
    nameRegex = re.compile(r'([A-Z]([a-z]{1,15})(\s{1,2})Nakamoto)')
    nameList = []
    result = nameRegex.findall(text)
    for x in range(len(result)):
        nameList.append(result[x][0])
    pyperclip.copy('\n'.join(nameList))
    return '\n'.join(nameList)


text = ''''Satoshi Nakamoto'

'Alice Nakamoto'

'Robocop Nakamoto'

but not the following:

'satoshi Nakamoto' (where the first name is not capitalized)

'Mr. Nakamoto' (where the preceding word has a nonletter character)

'Nakamoto' (which has no first name)

'Satoshi nakamoto' (where Nakamoto is not capitalized)'''
print(getName(text))

这个例子里面加入了pyperclip 复制到粘贴板的功能,需要安装粘贴板
可以用pip install pyperclip 直接安装
用pyperc.copy(’ \n’.join(nameList))可以将输出值复制到粘贴板,Ctril+v 可以粘贴到你想用的地方!
copy()函数需要将其他类型转换成str类型输出

猜你喜欢

转载自blog.csdn.net/tianpingxian/article/details/80307017
今日推荐