python 正则表达式匹配 指定的保护特征字符串 练习记录

import re

import pyperclip


def getNameOr(text):
    nameOrRegex = re.compile(r'((Alice|Bob|Carol)\s{1,2}(eats|pets|throws)\s{1,2}(apples|cats|baseballs))', re.I)
    nameOrList = []
    moResult = nameOrRegex.findall(text)
    for x in range(len(moResult)):
        nameOrList.append(moResult[x][0])
    pyperclip.copy('\n'.join(nameOrList))
    return '\n'.join(nameOrList)


text ='''following:

'Alice eats apples.'

'Bob pets cats.'

'Carol throws baseballs.'

'Alice throws Apples.'

'BOB EATS CATS.'

but not the following:

'Robocop eats apples.'

'ALICE THROWS FOOTBALLS.'

'Carol eats 7 cats.'''

print(getNameOr(text))

猜你喜欢

转载自blog.csdn.net/tianpingxian/article/details/80308283