一个正则表达式的问题

intelpirate 2012-03-16 05:58:14
想在total commander里用正则表达式实现这样一个功能
把分别以00001,00002,00003,……,00234为文件名的文件分开
把单数文件名的文件重新从00001开始排序并在最后加字母m,如00004m.
把双数文件名的文件重新从00001开始排序并在最后加字母n,如00004n.
这个搜索替换的正则表达式要怎么写。
...全文
216 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
兆帅 2012-12-19
  • 打赏
  • 举报
回复
引用 3 楼 libralibra 的回复:
引用 楼主 intelpirate 的回复:想在total commander里用正则表达式实现这样一个功能 把分别以00001,00002,00003,……,00234为文件名的文件分开 把单数文件名的文件重新从00001开始排序并在最后加字母m,如00004m. 把双数文件名的文件重新从00001开始排序并在最后加字母n,如00004n. 这个搜索替换的正则表……
++
crifan 2012-12-18
  • 打赏
  • 举报
回复
据我所知,无法实现你这样的功能。 可能的办法: 用各种脚本,比如python,写代码,去实现你这些需求;
libralibra 2012-12-17
  • 打赏
  • 举报
回复
引用 楼主 intelpirate 的回复:
想在total commander里用正则表达式实现这样一个功能 把分别以00001,00002,00003,……,00234为文件名的文件分开 把单数文件名的文件重新从00001开始排序并在最后加字母m,如00004m. 把双数文件名的文件重新从00001开始排序并在最后加字母n,如00004n. 这个搜索替换的正则表达式要怎么写。
这个用正则是给自己找罪受吗?把下面的range的结束改为235就行了
#! /usr/bin/env python
import os
for i in range(1,235):
    fname = '%05d' % i
    if os.path.isfile(fname):
        new_name = fname + (i%2==0 and 'm' or 'n')
        os.rename(fname,new_name)
print 'DONE'
zjs100901 2012-12-16
  • 打赏
  • 举报
回复
只用正则表达式很难,配合代码吧。
#!/usr/bin/env python
# -*- coding:utf-8 -*- 

import re
import os
filelist = ['00001', '00002', '00003', '00004', '00005', '00006', '00007', '00008', '00009', '00010']
pattm = '\d{4}(1|3|5|7|9)'  #奇数
pattn = '\d{4}(0|2|4|6|8)'  #偶数
mcount = 1
ncount = 1

for file in filelist:
	rsm = re.match( pattm, file )
	if rsm is not None:
		newfile = '%05dm' % mcount
		print file, '->', newfile
		os.rename( file, newfile )
		mcount += 1
	else :
		rsn = re.match( pattn, file )
		if rsn is not None:
			newfile = '%05dn' % ncount
			print file, '->', newfile
			os.rename( file, newfile )
			ncount += 1
Rlay_2 2012-03-16
  • 打赏
  • 举报
回复
文件名长度自己处理下吧,我下班了.回去看欧冠抽签

#! -*- encoding:UTF-8 -*-
import re
filelist = ['00001','00002','00003','00004','00005','00006','00007','00008','00009','00010']
pattm = '\d{4}(1|3|5|7|9)' #奇数
pattn = '\d{4}(0|2|4|6|8)' #偶数
rslist = []
mcount = 1
ncount = 1

for file in filelist:
rsm = re.match(pattm, file)
rsn = re.match(pattn, file)
if rsm is not None:
rslist.append(re.sub(pattm, (str(mcount) + 'm'), file))
mcount += 1
elif rsn is not None:
rslist.append(re.sub(pattn, str(ncount) + 'n', file))
ncount += 1

print rslist

37,720

社区成员

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

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