正则表达式匹配.avi文件名

  • 一万年没有来这里写东西了,今天分享一下刚刚完成的一个小功能:也就是标题所述的功能咯。【Java 实现】
  • 背景是这样子的:我在网上下载了很多视频文件。都是.avi结尾的。而且已经给我排序了,视频文件名称都是
    1.xxx.avi
    2.xxx.avi
    ...
    9.xxx.avi
    10.xxx.avi
    11.xxx.avi
    ...
    1024.xxx.avi
  • 播放器是智能的,知道按顺序播放。但是播放器不够智能的是,不能区分 1.xxx.avi10.xxx.avi100.xxx.avi的顺序。必须要是

    01.xxx.avi
    02.xxx.avi
    ...
    09.xxx.avi
    10.xxx.avi
    ...
    1024.xxx.avi

    这样的命名之后,播放器才能按照01-1024的顺序播放。如果1-1024,播放器就傻逼了。顺序就乱掉了。,所以,我需要做的就是,将所有的1.xxx.avi-9.xxx.avi全部重命名成01.xxx.avi -09.xxx.avi10 -1024开头的就不需要重命名了。

  • 我绞尽乳汁,终于想到了。关键代码就3行:

  • *
public static void main(String[] args) {
        boolean matches = "11.wererer.avi".matches("\\d\\..+\\.avi");
        System.out.println("----"+matches);
//      ----false
        boolean matches2 = "1.wererer.avi".matches("\\d\\..+\\.avi");
        System.out.println("----"+matches2);
//      ----true
    }
  • 实际代码呢,要判断一下参数,给文件重命名:
  • *
package com.pythoncat.hellomyeclipse;
import java.io.File;
import org.apache.commons.lang3.StringUtils;

public class FileRename {

    public static void main(String[] args) {
        reNameFiles("G:\\avi目录");
    }

    /**
     * 将视频文件1.xx.avi ,2.xx.avi ,...9.xx.avi,10.xx.avi 转换为01.xx.avi,02.xx.avi
     * ...10及以后的,不需要转换
     * 
     * @param dir
     */
    public static void reNameFiles(String dir) {
        System.out.println("输出 了。。。。。。。。。。。。。。。");
        boolean check = checkDirPath(dir);
        if (check) {
            File dirFile = new File(dir);
            if (dirFile.isDirectory()) {
                File[] files = dirFile.listFiles();
                for (File file : files) {
                    if (file.isDirectory()) {
                        reNameFiles(file.getPath());
                    } else {
                        reName(file);
                    }
                }
            } else {
                reName(dirFile);
            }

        }
    }

    private static void reName(File file) {
        // 1.SSSSSSSSSSSSSSS.avi

        String name = file.getName();
        if (!StringUtils.isEmpty(name)) {
            String rex = "\\d\\..+\\.avi";
            boolean matches = name.matches(rex);
            System.out.println("mat=====" + matches);
            if (matches) {
                // 说明是一个avi文件
                System.out.println("name = " + name);
                String path = file.getPath();
                File dest = new File(file.getParent(), "0"
                        + file.getName());
                boolean renameTo = file.renameTo(dest);
                System.out.println("renameTo = " + renameTo + "\treName = "
                        + dest.getName());
            }
        }
    }

    private static boolean checkDirPath(String dir) {
        if (StringUtils.isEmpty(dir)) {
            System.err.println("目标路径为空: " + dir);
            return false;
        }
        File dirFile = new File(dir);
        if (dirFile == null || !dirFile.exists()) {
            System.err.println("目标路径不存在: " + dir);
            return false;
        }
        return true;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中,可以使用re模块来进行正则表达式匹配。要进行正则表达式匹配,可以使用re.match、re.search或re.findall等函数。re.match函数从字符串的开始位置匹配正则表达式,re.search函数在字符串中寻找可以匹配的子串,而re.findall函数寻找所有匹配正则表达式的子串并返回一个列表。 在使用正则表达式时,有一些特殊字符需要注意转义,例如\、^、$等。为了方便编写正则表达式且不考虑转义问题,可以在正则表达式前加上前缀r,例如r'^py\001&'。 此外,正则表达式中还有一些特殊的字符类别,例如\s匹配任何空白字符,\d匹配一个数字,\D匹配一个非数字等。还可以使用限定符如*、+、?来指定匹配次数。 在使用正则表达式时,可以使用分组、替换等功能。re.compile函数可以编译正则表达式,re.split函数可以根据正则表达式进行切分字符串,re.sub函数可以将匹配到的子串替换为指定的字符串。 综上所述,Python中的正则表达式匹配可以通过re模块提供的函数来实现,具体的匹配方法和功能可以根据需要选择使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [python 正则匹配](https://blog.csdn.net/weixin_42553458/article/details/90729738)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Python 正则表达式匹配](https://blog.csdn.net/qq_40727946/article/details/103566439)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值