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类型输出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值