作者gwokae (喵先生)
看板java
标题Re: [问题] 格式比对?
时间Sat Nov 12 14:11:15 2011
Regex matches 是完全符合,如果要挖出符合的资料请用find,并搭配group服用。
大概是这样吧:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class My {
public static void main(String[] args) {
String input = "--------DATE AN\n"+
"---------------\n"+
"08/09/11 09:00";
Matcher m = Pattern.compile(
"(\\d+/\\d+/\\d+\\s\\d+:\\d+)").matcher(input);
while (m.find()) {
System.out.println(m.group(1));
}
}
}
※ 引述《williamsydu (william)》之铭言:
: timestring=temp.substring(2, 17);
: 由上面程式读取後 会产生下面字串
: --------DATE AN
: ---------------
: 08/09/11 09:00
: 上面的资料 目前只需要日期时间那个部分, 所以写了下面比对部分
: if(timestring.matches("[0-9]{2}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}"))
: {
: DATETIME = timestring;
: }
: 执行後,发现并不work
: 想请问一下 比对那个部份是不是写错了...
: 谢谢!!
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.34.162.157
1F:推 williamsydu:感谢 学到很多 11/12 18:24