显示空列表的正则表达式错误,尽管歌词字符串中存在该模式
Regex error showing an empty list although the pattern is there in lyrics string
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
。由于 RegEx 无法理解一个单词,因此您需要先解决这个问题。为此,您可以:
- 创建从单词到数字的映射
- 在文本上应用映射
- 对新文本使用正则表达式,获取数字
样本
import re
# mapping
word_digit_map = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
"ten": 10,
"eleven": 11,
"twelve": 12,
}
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
'''
# apply mapping
for word in word_digit_map.keys():
lyrics = lyrics.lower().replace(word, str(word_digit_map[word]))
# Use regex
xmasReExp = re.compile(r'\d+')
print(xmasReExp.findall(lyrics))
出局
['12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2']
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
。由于 RegEx 无法理解一个单词,因此您需要先解决这个问题。为此,您可以:
- 创建从单词到数字的映射
- 在文本上应用映射
- 对新文本使用正则表达式,获取数字
样本
import re
# mapping
word_digit_map = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
"ten": 10,
"eleven": 11,
"twelve": 12,
}
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
'''
# apply mapping
for word in word_digit_map.keys():
lyrics = lyrics.lower().replace(word, str(word_digit_map[word]))
# Use regex
xmasReExp = re.compile(r'\d+')
print(xmasReExp.findall(lyrics))
出局
['12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2']