[英]Split the string with different length
我有一個 String String s = " ABCD 1122-12-12"
,即<space or single digit number>+<any string>+<space>+<date in YYYY-mm-dd>
格式的日期。
String.split 方法或任何其他實用程序方法的正則表達式可以是什么來將上述字符串分成三部分
[0] = <space or single digit number> = " "
[1] = <string> = "ABCD"
[2] = <date in YYYY-mm-dd> = "1122-12-12"
正則表達式( |\\d)(.+) (\\d{4}-\\d{2}-\\d{2})
應該可以完成這項工作。
String input = " ABCD 1122-12-12";
Pattern pattern = Pattern.compile("( |\\d)(.+) (\\d{4}-\\d{2}-\\d{2})");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
String spaceOrDigit = matcher.group(1);
String string = matcher.group(2);
String date = matcher.group(3);
System.out.println("spaceOrDigit = '" + spaceOrDigit + "'");
System.out.println("string = '" + string + "'");
System.out.println("date = '" + date + "'");
}
輸出:
spaceOrDigit = ' '
string = 'ABCD'
date = '1122-12-12'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.