data=20120701
pattern=re.compile(r'data') --匹配规则 --这个地方如何写
match=pattern.findall(文件列表)
if match:
print 符合规则的文件
文件列表中的文件示例(mm11280020120701.txt,mg20120701.txt,system112720120701)
7 个解决方案
#1
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
http://topic.csdn.net/u/20120724/16/ffe1f14c-4e61-40c3-aa98-473c92c0421f.html?5433
"""
import os;
import re;
#inputList = [ "mm11280020120701.txt", "mg20120701.txt","system112720120701"];
fileList = "mm11280020120701.txt,mg20120701.txt,system112720120701"
#如果你确保整个文件名中,只有你的日期带数字,那么可以写为:
#dataPattern = r"[^\d]*\d+[^\d]*";
dataPattern = r"\d+";
pattern = re.compile(dataPattern); # --匹配规则 --这个地方如何写
matchedList = pattern.findall(fileList);
if matchedList:
print "matchedList=",matchedList;#matchedList= ['11280020120701', '20120701', '112720120701']
#2
pattern=re.compile(r'20120701')
#3
或者:
pattern=re.compile(r'(?<!\d)20120701(?!\d)')
#4
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import re
flist = ['123.txt', 'hlof124.c', 'yes123.txt', '123', 'no123',
'just_is_1_2.cpp']
date = input('input the date: ')
pattern = re.compile(r'.*' + date + '.*')
for x in flist:
match = pattern.search(x)
#print(x, match)
if match:
print(x)
#5
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import re
flist = ['123.txt', 'hlof124.c', 'yes123.txt', '123', 'no123',
'just_is_1_2.cpp']
date = input('input the date: ')
pattern = re.compile(date)
for x in flist:
match = pattern.search(x)
if match:
print(x)
#6
现在是这样的,因为有两个文件 zheng11280020120601.8811 和 zheng11280020120601.8811.ver 我只想要匹配到zheng11280020120601.8811 这个文件,该如何操作?
#7
ls *1 就可以了,最后面是1而不是.erv
#1
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
http://topic.csdn.net/u/20120724/16/ffe1f14c-4e61-40c3-aa98-473c92c0421f.html?5433
"""
import os;
import re;
#inputList = [ "mm11280020120701.txt", "mg20120701.txt","system112720120701"];
fileList = "mm11280020120701.txt,mg20120701.txt,system112720120701"
#如果你确保整个文件名中,只有你的日期带数字,那么可以写为:
#dataPattern = r"[^\d]*\d+[^\d]*";
dataPattern = r"\d+";
pattern = re.compile(dataPattern); # --匹配规则 --这个地方如何写
matchedList = pattern.findall(fileList);
if matchedList:
print "matchedList=",matchedList;#matchedList= ['11280020120701', '20120701', '112720120701']
#2
pattern=re.compile(r'20120701')
#3
或者:
pattern=re.compile(r'(?<!\d)20120701(?!\d)')
#4
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import re
flist = ['123.txt', 'hlof124.c', 'yes123.txt', '123', 'no123',
'just_is_1_2.cpp']
date = input('input the date: ')
pattern = re.compile(r'.*' + date + '.*')
for x in flist:
match = pattern.search(x)
#print(x, match)
if match:
print(x)
#5
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import re
flist = ['123.txt', 'hlof124.c', 'yes123.txt', '123', 'no123',
'just_is_1_2.cpp']
date = input('input the date: ')
pattern = re.compile(date)
for x in flist:
match = pattern.search(x)
if match:
print(x)
#6
现在是这样的,因为有两个文件 zheng11280020120601.8811 和 zheng11280020120601.8811.ver 我只想要匹配到zheng11280020120601.8811 这个文件,该如何操作?
#7
ls *1 就可以了,最后面是1而不是.erv