每天CookBook之Python-023

  • 实现shell风格匹配字符串
  • fnmatch匹配字符串(忽略大小写)
  • fnmatchcase匹配字符串
from fnmatch import fnmatch, fnmatchcase

print(fnmatch('foo.txt', '*.txt'))
print(fnmatch('foo.txt', '?oo.txt'))
print(fnmatch('dat45.txt', 'dat[0-9][0-9].txt'))

names = ['Dat1.csv', 'Dat2.csv', 'config.ini', 'foo.py']

print([name for name in names if fnmatch(name, 'Dat*.csv')])

print(fnmatch('foo.txt', '*.TXT'))
print(fnmatchcase('foot.txt', '*.TXT'))

addresses = [
    '5412 N CLARK ST',
    '1060 W ADDISON ST',
    '1039 W GRANVILLE AVE',
    '2122 N CLARK ST',
    '4802 N BROADWAY',
]

print([addr for addr in addresses if fnmatchcase(addr, '* ST')])
print([addr for addr in addresses if fnmatchcase(addr, '54[0-9][0-9] *CLARK*')])
True
True
True
['Dat1.csv', 'Dat2.csv']
True
False
['5412 N CLARK ST', '1060 W ADDISON ST', '2122 N CLARK ST']
['5412 N CLARK ST']
posted @ 2016-07-12 22:48  4Thing  阅读(70)  评论(0编辑  收藏  举报