[英]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
是天,但是如果将其取反,则需要将日期格式字符串中的yy
和dd
反。)
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 (...)
是一个捕获组,表示...
是您要捕获的模式
\\\\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]
匹配的捕获组 $
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.