我在sqlite数据库中有一个数据。我需要匹配遵循这些规则的单元格值 -
1. 355 2. 35,36 3. 35,36,355 4. 45,35***,355 5. 35 6. 35* 7. 45,35* 8. 50 9. 45,36 10. 151943 11. 5,355*,5 12. 3,5 13. 3, 35**, 5
Here output should be line number 2, 3, 4, 5, 6, 7, 13.
What should be the ideal regular expression for this.
import re def match(expr, item): x = re.match(expr, item) if x is not None: return True items=['355','35,36','35,36,355','45,35***,355','35','35*','45,35*','50','45,36','151943','5,355*,5','3,5','3500,57','351,35**,4'] for item in items: if match('.*,?(35)\**,?.*', item): print item
答案 0 :(得分:0)
如果我们没有正则表达式怎么办?
# Recognize valid items
def valid(item):
VALUE = "35"
values = item.replace("*", "").split(",")
return VALUE in values
# Display valid items
for item in items:
if valid(item):
print item
简单地说,我们删除所有星号,我们创建一个单独的值列表,然后检查那里是否存在预期值。