6

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

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()”为真时,它在第一次迭代中返回 [00:02:10],在第二次迭代中返回 [00:04:50]。

4

4 回答 4

7

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

"^(\\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]
于 2013-09-26T13:41:45.343 回答
4

我是用这种方式做的。

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;
            }                  
        }
于 2013-09-26T13:54:42.177 回答
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 为DateFormat.parse

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

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

于 2013-09-26T13:44:07.180 回答
3
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-09-26T13:52:17.603 回答