码迷,mamicode.com
首页 > 其他好文 > 详细

在一串字符串中找到与正则表达式匹配的字符串?(例如:export_20170717_out.log 找到20170717)

时间:2017-07-17 11:47:08      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:int   .com   ring   mat   字符串   字符   static   div   exp   

 

如题:提取字符串:export_20170717_out.log

   对应的日期:20170717

package dodo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class demo {
    static String cmd="export_20170717_out.log";
    public static void main(String[] args) {
        String regex="\\d{8}";
        Pattern pa=Pattern.compile(regex);
        Matcher ma=pa.matcher(cmd);
        if(ma.find()){
            System.out.println("取到的日期为:"+ma.group(0));
        }
    }

}

输出结果为:

取到的日期为:20170717

 

在一串字符串中找到与正则表达式匹配的字符串?(例如:export_20170717_out.log 找到20170717)

标签:int   .com   ring   mat   字符串   字符   static   div   exp   

原文地址:http://www.cnblogs.com/ranrongzhen/p/7193511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!