在我的应用程序中,我从服务器收到一些响应。我得到的时间格式是“PT10H2M1S”,“PT1H2S”,“PT0S”等
。如果字符串中存在 H
,我必须将其之前的数字视为小时。如果字符串中存在 M
,我必须将其之前的数字视为分钟。如果字符串中存在 S
,我必须将其之前的数字视为秒。我该怎么做?
最佳答案
static Pattern p = Pattern.compile("PT(?:([0-9]+)H)?(?:([0-9]+)M)?(?:([0-9]+)S)?");
static void parse(String s){
Matcher m = p.matcher(s);
m.find();
String hh = m.group(1);
String mm = m.group(2);
String ss = m.group(3);
System.out.println("s="+s+" hh="+hh+" mm="+mm+" ss="+ss);
}
关于java - java中如何进行字符串的模式匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30497850/