在下面的错误代码中,我希望找到每个匹配的以下信息。
\w+
或\d+
。我想用两个以上的基本选择。是否可以不使用蛮力?
#! /usr/bin/env python3
import re
pattern = re.compile(r"(\w+)|(\d+)")
text = "a word or 2, or three"
for x in pattern.findall(text):
print(x)
('a', '')
('word', '')
('or', '')
('2', '')
('or', '')
('three', '')
('a', 0, '(\w+)')
('word', 2, '(\w+)')
('or', 7, '(\w+)')
('2', 10, '(\d+)')
('or', 13, '(\w+)')
('three', 16, '(\w+)')
答案 0 :(得分:3)
感谢所有评论,我有以下解决方案。
import re
text = "a word or 2, or three"
print('---')
pattern = re.compile(r"(?P<word>[^\W\d_]+)|(?P<digits>\d+)")
for m in pattern.finditer(text):
name = next(k for k, v in m.groupdict().items() if v)
print((m.group(), m.start(), name))
print('---')
pattern = re.compile(r"(?P<one_word>[^\W\d_]+)|(?P<some_digits>\d+)")
for m in pattern.finditer(text):
name = next(k for k, v in m.groupdict().items() if v)
print((m.group(), m.start(), name))
---
('a', 0, 'word')
('word', 2, 'word')
('or', 7, 'word')
('2', 10, 'digits')
('or', 13, 'word')
('three', 16, 'word')
---
('a', 0, 'one_word')
('word', 2, 'one_word')
('or', 7, 'one_word')
('2', 10, 'some_digits')
('or', 13, 'one_word')
('three', 16, 'one_word')