python 正则表达式匹配 指定的保护特征字符串 练习记录 ╰半橙微兮° 2022-05-25 11:39 97阅读 0赞 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))
还没有评论,来说两句吧...