Java 捕获以不同格式显示的字符串内容信息
我正在接收三种不同格式的XML标记内容 格式-1:Java 捕获以不同格式显示的字符串内容信息,java,Java,我正在接收三种不同格式的XML标记内容 格式-1: <description> START_TIME: 1400 HOURS FINISH_TIME: 1000 HOURS</description> 标记是重复标记。另一个标记的值为START DATE和FINISH DATE您可以使用正则表达式: -?START([^:]+):([0-9 ]+)(HOURS)?\s*FINISH(\1):([0-9 ]+)(HOURS)? 看到这个了吗 和java代码: S
<description> START_TIME: 1400 HOURS FINISH_TIME: 1000 HOURS</description>
标记是重复标记。另一个标记的值为START DATE和FINISH DATE您可以使用
正则表达式
:
-?START([^:]+):([0-9 ]+)(HOURS)?\s*FINISH(\1):([0-9 ]+)(HOURS)?
看到这个了吗
和java代码:
String[] strs = {
"START_TIME: 1400 HOURS FINISH_TIME: 1000 HOURS",
"START-TIME: 1400 HOURS FINISH-TIME: 1100 HOURS",
"-STARTTIME: 1400 FINISHTIME: 1100"};
String regex = "-?START([^:]+):([0-9 ]+)(HOURS)?\\s*FINISH(\\1):([0-9 ]+)(HOURS)?";
Pattern p = Pattern.compile(regex);
for (String str : strs) {
Matcher m = p.matcher(str);
if (m.find()) {
String start = m.group(2).trim();
String finish = m.group(5).trim();
System.out.println("START:" + start + " FINISH:" + finish);
}
}
我认为,
if(description.contains(“开始”)&&description.contains(“完成”)
应该有效,因为缺少的信息标记是重复标记。另一个标记带有值START DATE和FINISH DATE。ThanksOK然后if(description.contains(“开始”)&&description.contains(“完成”)&&description.contains(“时间”)
应该有效谢谢你的回答。对于起始值,我应该在“:”上做一个子字符串,不是吗?
String description= getNodeValue(node, ".");
if (description.contains("-STARTTIME:") && description.contains("FINISHTIME:") || description.contains("START-TIME:") && description.contains("FINISH-TIME:") || description.contains("-STARTTIME:") && description.contains("FINISHTIME:")) {
//Found the content
-?START([^:]+):([0-9 ]+)(HOURS)?\s*FINISH(\1):([0-9 ]+)(HOURS)?
String[] strs = {
"START_TIME: 1400 HOURS FINISH_TIME: 1000 HOURS",
"START-TIME: 1400 HOURS FINISH-TIME: 1100 HOURS",
"-STARTTIME: 1400 FINISHTIME: 1100"};
String regex = "-?START([^:]+):([0-9 ]+)(HOURS)?\\s*FINISH(\\1):([0-9 ]+)(HOURS)?";
Pattern p = Pattern.compile(regex);
for (String str : strs) {
Matcher m = p.matcher(str);
if (m.find()) {
String start = m.group(2).trim();
String finish = m.group(5).trim();
System.out.println("START:" + start + " FINISH:" + finish);
}
}