java - 如何从这个字符串中解析出时间戳?

标签 java regex string parsing

我有这个示例字符串:

Sample string 1:
A^1.1#B^1#I^2#f^0#p^1#d^2010-07-21T08:52:05.222ZKHBDGSLKHFBDSLKFGNIF#%$%^$#^$XLGCREWIGMEWCERG

Sample string 2:
A^1.1#B^1#f^0#p^1#d^2010-07-22T07:02:05.370ZREGHCOIMIYR$#^$#^$#^EWMGCOINNNNNNVVVRFGGYVJ667VTG

因此,我需要从这些字符串中取出时间戳:

2010-07-21T08:52:05.222 or
2010-07-22T07:02:05.370

基本上是黑白值d^和Z

最好的(“最聪明的”)方法是什么?子字符串(),正则表达式?

最佳答案

Pattern p = Pattern.compile("(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3})");
//you could also use "d\\^(.*)Z" as your regex patern
Matcher m = p.matcher("your string here");

if (m.find()) {
    System.out.println(m.group(1)); //print out the timestamp
}

取自 here

此外,如果您要循环访问一系列字符串,请确保重用 Pattern p 对象

关于java - 如何从这个字符串中解析出时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3302870/

相关文章:

java - 在Java中实现管道

java - 将图像从网站加载到 Android 应用程序

java - 欧拉计划 #17 错误答案

java 正则表达式 : capture multiline sequence between tokens

ruby - 正则表达式 ruby gsub : inserting space between characters in capture group

java - StringBuilder - 完全替换现有字符串。有什么方法吗?

string - 如何在 Matlab 中为文本添加轮廓?

java - 如何找出使用过的 JAR?

Python Challenge Lvl 3 解释(警告剧透!)

c - 我想打印用户输入的名称和名称前后的一些星星(也输入)