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

cenhimgn13 2012-07-24 04:46:40
根据日期来匹配文件,将符合规则的列出来,输入参数20120701

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

文件列表中的文件示例(mm11280020120701.txt,mg20120701.txt,system112720120701)
...全文
755 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
ls *1 就可以了,最后面是1而不是.erv
cenhimgn13 2012-07-27
  • 打赏
  • 举报
回复
现在是这样的,因为有两个文件 zheng11280020120601.8811 和 zheng11280020120601.8811.ver 我只想要匹配到zheng11280020120601.8811 这个文件,该如何操作?
iEverX 2012-07-25
  • 打赏
  • 举报
回复

#! /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)
iEverX 2012-07-25
  • 打赏
  • 举报
回复
#! /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)
bugs2k 2012-07-24
  • 打赏
  • 举报
回复
或者:

pattern=re.compile(r'(?<!\d)20120701(?!\d)')
bugs2k 2012-07-24
  • 打赏
  • 举报
回复
pattern=re.compile(r'20120701')
crifan 2012-07-24
  • 打赏
  • 举报
回复

#!/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']

37,722

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧