python括号如何转义_Python正则表达式不转义字母数字字符串中的圆括号,并将符号和。。。
import re, os, sys, shutil
def readDir(path1):
return [ f for f in os.listdir(path1) if os.path.isfile(os.path.join(path1,f)) ]
def files(dir1,term,path1):
match2 = []; stillWrong = []#; term = str(term)
for f in dir1:
result = re.search(term + "\s\b\s\(\d{5}\)\s\b\s\w{3}\s\d{4}\b\d{3}[a-z\A-Z]+\s\b\s[A-z\a-z]+\b\s[A-Z\a-z]+ .txt",f)
if result: match2.append(f)
else: stillWrong.append(f)
#print "split --- ",os.path.split(f)
##else: os.rename(path1+'\\'+f, path1+'\\'+'@ '+f); stillWrong.append(f)
print "f ---- ",f
return match2, stillWrong
正则表达式获取括号内容term = "201308"; src = "testdir1"; dest = "testdir2"
print files(readDir(dest),term,dest)
这就产⽣了(显然)错误:
^{pr2}$
如您所见,match2[]列表中没有任何内容(如果您感兴趣,这些是第⼆个列表中的⽂件名,但是第⼀个列表包含相关的匹配项)。我在⾃学Python和regex,但进展并不顺利。我尝试过这些(和regex教程),但在这种情况下似乎没有帮助:
所有的@都来⾃于被注释掉的os.rename,但在被注释之前它还是不起作⽤。我相信任何初级程序员都可以在⼏分钟内完成这项⼯作,但如果⼀个专业⼈⼠能在这个问题上花上⼀分钟,那也太好了。在
编辑:
使⽤的⽂件名列表(⽣产列表显然更长):201308-(12345) - Abc 2233-007-course Name - last,
201308 - (82608) - MAC 2233-007-Methods of Calculus - Klingler,
@ @ @ @ @ @ 201308
@ @ @ @ @ @ 123 abc - a-1 -
@ @ @ @ @ @ 45-12 - xyz - mno -
@ @ @ @ @ @ @ @ @ 201308-(12345) - Abc 2233-007-course Name - last,
@ @ @ @ @ @ @ @ @ 201308
@ @ @ @ @ @ @ @ @ 201308 - (82608) - MAC 2233-007-Methods of Calculus - Klingler,
@ @ @ @ @ @ @ @ @ @ 123 abc - a-1 -
@ @ @ @ @ @ @ @ @ @ 45-12 - xyz - mno -
@ @ @ @ @ @ @ @ @ @ @ xxxxx xxxxx xxxxx 123 abc - a-1 -
@ @ @ @ @ @ @ @ @ @ @ xxxxx xxxxx xxxxx 45-12 - xyz - mno - 45-12 - xyz - mno -
123 abc - a-1 -
201308 - (82608) - MAC 2233-007-Methods of Calculus - Klingler,
201308
201308-(12345) - Abc 2233-007-course Name - last,

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。