【问题标题】:Java Regular Expression Matching for hh:mm:ss in String字符串中 hh:mm:ss 的 Java 正则表达式匹配
【发布时间】:2013-09-26 13:37:03
【问题描述】:

我正在解析一个文件,其中包含基于时间的条目。格式如下:

00:02:10-XYZ:Count=10
00:04:50-LMK:Count=3

这里我想要的是从字符串行中提取时间值

我搜索了很多链接都找不到我想要的东西,最终我写了这段代码。

    Pattern pattern = Pattern.compile("((?i)[0-9]{1,2}:??[0-9]{0,2}:??[0-9]{0,2})"); //(?i)[0-9]{1,2}:??[0-9]{0,2}:??[0-9]{0,2}  //\\d{1,2}:\\d{1,2}:\\d{1,2}
    Matcher matcher;
    List<String> listMatches;

下面是我应用逻辑的循环

    for(int x = 0; x < file_content.size(); x++)
    {
            matcher= pattern.matcher(file_content.get(x));
            listMatches = new ArrayList<String>();
            while(matcher.find())
            {
                listMatches.add(matcher.group(1));
                break;
            }
     }

我希望当“matcher.find()”为 true 时,它​​会在第一次迭代中返回 [00:02:10],在第二次迭代中返回 [00:04:50]。

【问题讨论】:

  • 您是否考虑过使用SimpleDateFormat 而不是自己编写正则表达式?
  • 不,先生,我没有,感谢您分享有价值的代码

标签: java regex time


【解决方案1】:

看起来像一个不必要的复杂模式......为什么不只是(如果你正在做逐行处理):

"^(\\d\\d:\\d\\d:\\d\\d)"

如果你在做多行处理,你会想要使用:

"(?m)^(\\d\\d:\\d\\d:\\d\\d)"

这是一些示例代码和输出:

public static void main(String[] args) {
    final Pattern pattern = Pattern.compile("(?m)^(\\d\\d:\\d\\d:\\d\\d)");
    final Matcher matcher = pattern.matcher("00:02:10-XYZ:Count=10\n00:04:50-LMK:Count=3");
    while(matcher.find())
    {
        System.out.printf("[%s]\n", matcher.group(1));
    }        
}

输出

[00:02:10]
[00:04:50]

【讨论】:

  • 99:99:99 这样的输入呢?您的正则表达式匹配非时间值:( p.s. 为什么对匹配进行分组?您知道您可以只获得零组吗?
【解决方案2】:

我就是这样做的。

00:02:10-XYZ:Count=10
00:04:50-LMK:Count=3

Pattern pattern = Pattern.compile("([2][0-3]|[0-1][0-9]|[1-9]):[0-5][0-9]:([0-5][0-9]|[6][0])");
//File Beginning Time
for(int x = 0; x < file_content.size(); x++)
   {
        matcher= pattern.matcher(file_content.get(x));
        ListMatches = new ArrayList<String>();
        if(matcher.find())
          {
                start_time = matcher.group();
                break;
          }                
    }
//File End Time
for(int x = file_content.size()-1; x > 0 ; x--)
        {
            matcher= pattern.matcher(file_content.get(x));
            listMatches = new ArrayList<String>();
            if(matcher.find())
            {
                end_time = matcher.group();
                break;
            }                  
        }

【讨论】:

    【解决方案3】:

    不要为此使用正则表达式,使用SimpleDateFormat。这有两个巨大的优势

    1. SimpleDateFormat 中的代码经过测试且稳健
    2. SimpleDateFormat 将进行验证以确保您拥有实时号码

    这看起来像这样:

    public static void main(String[] args) throws Exception {
        final String s = "00:02:10-XYZ:Count=10\n"
                + "00:04:50-LMK:Count=3";
        final Scanner sc = new Scanner(s);
        final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        while(sc.hasNextLine()) {
            final String line = sc.nextLine();
            final Date date = dateFormat.parse(line);
            final Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            System.out.println(calendar.get(Calendar.HOUR));
            System.out.println(calendar.get(Calendar.MINUTE));
            System.out.println(calendar.get(Calendar.SECOND));
        }
    }
    

    输出:

    0
    2
    10
    0
    4
    50
    

    来自javadoc for DateFormat.parse

    从给定字符串的开头解析文本以生成日期。 该方法可能不会使用给定字符串的整个文本。

    因此SimpleDateFormat 将解析String,直到它读取指定的整个模式然后停止。

    【讨论】:

    • 好像应该是("KK:mm:ss")
    • @Anirudh,你是对的 - 它不应该是 hh,但我认为应该是 HH
    【解决方案4】:
    SimpleDateFormat dateFormat = new SimpleDateFormat("KK:mm:ss");    
    Pattern pattern = Pattern.compile("\\d+:\\d+:\\d+");
    Matcher matcher;
    List<Date> listMatches = new ArrayList<Date>();
    for(int x = 0; x < file_content.size(); x++)
    {
        matcher= pattern.matcher(file_content.get(x));
        while(matcher.find())
        {
            Date temp=null;
            try{temp=dateFormat.parse(matcher.group(0));}catch(ParseException p){}
            if(temp!=null)
            listMatches.add(temp);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2013-12-25
      相关资源
      最近更新 更多