import re
lyrics = '''Twelve drummers drumming, eleven pipers piping
Ten lords a leaping, nine ladies dancing, eight maids a milking
Seven swans a swimming, six geese a laying, five gold rings
Four calling birds, three French hens
Two turtle doves and a partridge in a pear tree
'''
xmasReExp = re.compile(r'\d+\s\w+')
print(xmasReExp.findall(lyrics))
结果是正确的,即空列表
\d
表示数字,而lyrics
中没有匹配的模式如果我理解正确,您希望查找文本
lyrics
中用单词书写的数字。由于正则表达式无法理解一个单词,因此需要首先解决这个问题。为此,您可以:样本
Out
相关问题 更多 >
编程相关推荐