读书人

python 正则表达式婚配,日期匹配文件

发布时间: 2012-09-27 11:11:17 作者: rapoo

python 正则表达式匹配,日期匹配文件
根据日期来匹配文件,将符合规则的列出来,输入参数20120701

data=20120701
pattern=re.compile(r'data') --匹配规则 --这个地方如何写
match=pattern.findall(文件列表)
if match:
print 符合规则的文件

文件列表中的文件示例(mm11280020120701.txt,mg20120701.txt,system112720120701)

[解决办法]
[code=python]
#!/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']
[/code]
[解决办法]

Python code
pattern=re.compile(r'20120701')
[解决办法]
C/C++ code
#! /usr/bin/env python3# -*- coding: utf-8 -*-import reflist = ['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) 

读书人网 >perl python

热点推荐