我有可能包含或不包含要解析的日期的字符串(例如,使用类似java.time.format.DateTimeFormatter的类(自java 8开始)或java.text.DateFormat)
示例:
搜索格式
中-
的日期 在字符串yyMMdd HH-mm-ss
日期格式可能有所不同,因此我无法使用预定义的正则表达式。
在String中搜索如此日期的最佳方法是什么? 例如,可以从DateTimeFormatter获取正则表达式吗?
我唯一的想法是计算This was happend at: 140327 12-08-12, yeah.
,y
,M
等等,为它创建一个正则表达式,但它看起来像我上面的例子那样难看......
修改
现在,日期解析类已经过精确处理了。
但确切的解析器并不是那么重要: 我需要一些日期解析器模式和正则表达式模式来搜索String中的日期,而不是解析找到的String。
解析器模式使用的确切语法也不是很重要,它可以使用另一种语法,如上例所示。
我不知道编译时的确切日期格式。用户将输入日期格式模式(如d
),而不是我必须搜索在String中使用此模式的日期。 (我可以编写一个自己的函数来将日期格式模式转换为正则表达式模式但是......也许这已经存在了吗?)
答案 0 :(得分:1)
您只提供了日期所在的格式。
([0-9]{2})([0-9]{2})([0-9]{2})\s+([0-9]{2})-([0-9]{2})-([0-9]{2})
此表达式将:
yyMMdd hh-mm-ss
现场演示
https://regex101.com/r/oR3dJ0/1
示例文字
示例:搜索格式的日期" yyMMdd HH-mm-ss"在字符串"发生在:140327 12-08-12,是的。"
样本匹配
MATCH 1
0. [95-110] `140327 12-08-12`
1. [95-97] `14`
2. [97-99] `03`
3. [99-101] `27`
4. [102-104] `12`
5. [105-107] `08`
6. [108-110] `12`
NODE EXPLANATION
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
( group and capture to \3:
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
) end of \3
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \4:
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
) end of \4
----------------------------------------------------------------------
- '-'
----------------------------------------------------------------------
( group and capture to \5:
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
) end of \5
----------------------------------------------------------------------
- '-'
----------------------------------------------------------------------
( group and capture to \6:
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
) end of \6
----------------------------------------------------------------------
答案 1 :(得分:1)
要根据用户所需的日期时间格式构造正则表达式,我只需要使用它们的格式字符串,并用已知的正则表达式替换各种块。换句话说,如果他们输入yy
,则表示正则表达式[0-9]{2}
,而yyyy
表示[0-9]{4}
等。
现场演示
Java代码
class Main {
public static void main(String[] args) {
System.out.println( convert("yyyyMMdd") );
System.out.println( convert("yyyy-MM-dd") );
System.out.println( convert("yyyyMMdd HH:mm") );
System.out.println( convert("yyyyMMdd HH:mm:ss") );
System.out.println( convert("yyyy MMM dd") );
}
public static String convert(String original) {
String Output = original;
System.out.println("");
System.out.println(original);
// Year
Output = Output.replaceAll("yyyy", "[0-9]{4}");
Output = Output.replaceAll("yy", "[0-9]{2}");
// Month
Output = Output.replaceAll("MMMM", "(?:January|Feburary|March|April|May|June|July|August|September|October|November|December)");
Output = Output.replaceAll("MMM", "(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)");
Output = Output.replaceAll("MM", "(?:0[0-9]|1[0-2])"); // 00-12
// Day
Output = Output.replaceAll("dddd", "(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)");
Output = Output.replaceAll("ddd", "(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)");
Output = Output.replaceAll("dd", "(?:[0-2][0-9]|3[01])"); // 00-31
// Hour
Output = Output.replaceAll("HH", "(?:[0-1][0-9]|2[0-3])"); // 24 hour format
Output = Output.replaceAll("hh", "(?:0[0-9]|1[0-2])"); // 12 hour format
// Minutes
Output = Output.replaceAll("mm", "[0-5][0-9]"); // 0-59
// Seconds
Output = Output.replaceAll("ss", "[0-5][0-9]"); // 0-59
// Meridian
Output = Output.replaceAll("EE", "(?:AM|PM)"); // AM or PM
Output = Output.replaceAll("ee", "(?:am|pm)"); // am or pm
// System.out.println(Output);
return Output;
}
}
示例输出
yyyyMMdd
[0-9]{4}(?:0[0-9]|1[0-2])(?:[0-2][0-9]|3[01])
yyyy-MM-dd
[0-9]{4}-(?:0[0-9]|1[0-2])-(?:[0-2][0-9]|3[01])
yyyyMMdd HH:mm
[0-9]{4}(?:0[0-9]|1[0-2])(?:[0-2][0-9]|3[01]) (?:[0-1][0-9]|2[0-3]):[0-5][0-9]
yyyyMMdd HH:mm:ss
[0-9]{4}(?:0[0-9]|1[0-2])(?:[0-2][0-9]|3[01]) (?:[0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]
yyyy MMM dd
[0-9]{4} (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (?:[0-2][0-9]|3[01])