简体   繁体   English

在Java中使用正则表达式从字符串中提取日期,格式为01OCT12 14:26

[英]Using regular expression in Java to extract date from string in the format 01OCT12 14:26

I am looking to read dates from either a file or String in the format 01OCT12 14:26 我正在从文件或字符串中读取日期,格式为01OCT12 14:26

I know the dates will be of either the month OCT or NOV which may help me write a more precise regex expression. 我知道日期将是OCT或11月,这可能有助于我编写更精确的正则表达式。

I have read of quite a few option Java provides for using regex such as Matcher & Pattern and also the Scanner class and was hoping for help to find the cleanest way of tackling this. 我已经读过Java提供的许多使用正则表达式的选项,例如Matcher&Pattern以及Scanner类,希望能找到帮助解决该问题的最简洁方法。

If you know where in the string the date is, you could use a SimpleDateFormat for parsing instead of using a regex: 如果您知道日期在字符串中的何处,则可以使用SimpleDateFormat进行解析,而不要使用正则表达式:

DateFormat dateFormat = new SimpleDateFormat("yyMMMdd kk:mm", Locale.ENGLISH);
Date result = df.parse(stringDate);  

(I assumed that 01 was the year and 12 was the day, but if it's reversed, then you would need to reverse the yy and dd in the date format string.) (我假设01是年, 12是天,但是如果将其取反,则需要将日期格式字符串中的yydd反。)

Here's how to do it with regular expressions, with an example: 下面是使用正则表达式的示例:

        String example = "01OCT12 14:26";
        String pattern = "^(\\d{2})(?:OCT|NOV)(\\d{2}) (\\d{1,2}):(\\d{2})$";

        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(example);

        if (m.find()) 
        {
            //prints example
            System.out.println(m.group());

            System.out.println(m.group(1)); //prints 01
            System.out.println(m.group(2)); //prints 12
            System.out.println(m.group(3)); //prints 14
            System.out.println(m.group(4)); //prints 26
        }

"^(\\\\d{2})(?:OCT|NOV)(\\\\d{2}) (\\\\d{1,2}):(\\\\d{2})$" breakdown: "^(\\\\d{2})(?:OCT|NOV)(\\\\d{2}) (\\\\d{1,2}):(\\\\d{2})$"细分:

  • ^ means begins with ^表示从
  • (...) is a capturing group, meaning that ... is a pattern that you want to capture (...)是一个捕获组,表示...是您要捕获的模式
    • In this case, the pattern is \\\\d{2} , which means two of any digit in [0-9] 在这种情况下,模式为\\\\d{2} ,表示[0-9]中任意数字的两位
  • (?:OCT|NOV)
    • ?: means do NOT capture the following ?:表示不捕获以下内容
    • OCT|NOV means must be either OCT or NOV OCT|NOV手段必须是OCT或NOV
  • (\\\\d{1,2}) is a capture group that matches 1 or 2 numbers [0-9] (\\\\d{1,2})是与1或2个数字[0-9]匹配的捕获组
  • Finally, $ means ends with 最后, $表示以

You may not want to use regular expressions to accomplish this task; 您可能不希望使用正则表达式来完成此任务。 nonetheless, I believe that this is what you were looking for. 尽管如此,我相信这就是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式,用于从Java字符串中提取单词中指定的日期值 - Regular expression to extract Date value specified in word from the string in Java 使用正则表达式从日期格式字符串中删除元素 - Remove elements from Date Format String using a Regular Expression 在Java中使用正则表达式从字符串中提取信息 - Using Regular Expression in Java to extract information from a String 使用Java中的正则表达式从字符串中提取双精度或整数? - extract a double or integer from a string using regular expression in java? 正则表达式Java从字符串中提取余额 - Regular expression java to extract the balance from a string 使用正则表达式从字符串中提取数据 - Extract data from String using regular Expression 如何以以下方式将日期字符串“ 2013-08-26T12:00:00.000”格式化为“ 2013-08-26 12:00:00”到Java中的Date对象? - How to format this date string “2013-08-26T12:00:00.000” in the following way: “2013-08-26 12:00:00” to Date object in java? 使用正则表达式提取字符串 - Extract string using a regular expression 如何将 2019 年 10 月 9 日 12:00:00:000AM 的日期字符串格式化为 dd/MM/yyyy - How to format date string of Oct 9 2019 12:00:00:000AM to dd/MM/yyyy java.util.Date 返回 --> 2016-12-26 14:18:57.0 想要删除这个 .0 - java.util.Date returning --> 2016-12-26 14:18:57.0 want to remove this .0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM