[英]Java - extract date from string using regex- failing
我正在嘗試使用正則表達式從字符串中提取2個日期 - 由於某種原因 - 正則表達式不提取日期 - 這是我的代碼:
private String[] getDate(String desc) {
int count=0;
String[] allMatches = new String[2];
Matcher m = Pattern.compile("(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d(?:,)").matcher(desc);
while (m.find()) {
allMatches[count] = m.group();
}
return allMatches;
}
我的字符串desc是: "coming from the 11/25/2009 to the 11/30/2009"
,我得到了一個空數組......
你的正則表達式首先匹配日期和月份(DD / MM / YYYY),而輸入則以月份和日期(MM / DD / YYYY)開頭。
此外,您的日期必須后跟逗號匹配( (?:,)
部分)。
這個應該適合您的需求:
(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d
Debuggex圖。
3個問題:
1)您正在嘗試使用格式dd/MM/YYYY
解析日期,其中正則表達式的格式為MM/dd/YYYY
。
2)你忘了在while循環中增加count
。
3)正則表達式末尾的(?:,)
部分是無用的。
此代碼適用於我的計算機:
private static String[] getDate(String desc) {
int count=0;
String[] allMatches = new String[2];
Matcher m = Pattern.compile("(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d").matcher(desc);
while (m.find()) {
allMatches[count] = m.group();
count++;
}
return allMatches;
}
測試 :
public static void main(String[] args) throws Exception{
String[] dates = getDate("coming from the 25/11/2009 to the 30/11/2009");
System.out.println(dates[0]);
System.out.println(dates[1]);
}
輸出 :
25/11/2009
30/11/2009
你有一個月中的月份和日期,並且(?:,)
需要在每個日期結束時使用逗號。 試試這個:
(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d
日期模式識別算法,不僅識別日期模式,還以Java日期格式提取可能的日期。 該算法非常快速且輕量級。 處理時間是線性的,所有日期都在一次通過中識別。 算法使用樹遍歷機制解析日期。 自定義創建樹數據結構以構建支持的日期,時間和月份模式。
該算法還確認日期文字之間的多個空格字符。 例如DD DD DD和DD DD DD被視為有效日期。
以下日期模式被認為是有效的,並且可以使用該算法識別。
dd MM(MM)yy(yy)yy(yy)MM(MM)dd MM(MM)dd yy(yy)
其中M是月份字面值是字母格式,如Jan或January
日期之間允許的分隔符是'/','\\','',',','|',' - ',''
它還以下列格式識別尾隨時間模式hh(24):mm:ss.SSS am / pm hh(24):mm:ss am / pm hh(24):mm:ss am / pm
分辨率時間是線性的,不使用模式匹配或強力。 此算法基於樹遍歷並返回,日期列表包含以下三個組件 - 文本中標識的日期字符串 - 轉換和格式化日期字符串 - SimpleDateFormat
使用日期字符串和格式字符串,用戶可以根據需要自由地將字符串轉換為對象。
算法庫可在maven central獲得。
<dependency>
<groupId>net.rationalminds</groupId>
<artifactId>DateParser</artifactId>
<version>0.3.0</version>
</dependency>
下面是使用它的示例代碼。
import java.util.List;
import net.rationalminds.LocalDateModel;
import net.rationalminds.Parser;
public class Test {
public static void main(String[] args) throws Exception {
Parser parser=new Parser();
List<LocalDateModel> dates=parser.parse("Identified date :'2015-January-10 18:00:01.704', converted");
System.out.println(dates);
}
}
輸出:[LocalDateModel {originalText = 2015-january-10 18:00:01.704,dateTimeString = 2015-1-10 18:00:01.704,conDateFormat = yyyy-MM-dd HH:mm:ss.SSS,start = 18,端46 =}]
詳細博客http://coffeefromme.blogspot.com/2015/10/how-to-extract-date-object-from-given.html
完整的源代碼可以在GitHub上獲得, 網址為https://github.com/vbhavsingh/DateParser
LocalTime.parse
而不是正則表達式 對於這樣的問題,正則表達式可能有點過分。
您可以在SPACE字符上拆分字符串,並嘗試將每個元素解析為LocalDate
。 如果解析失敗,請轉到下一個元素。
String input = "coming from the 11/25/2009 to the 11/30/2009" ;
String[] elements = input.split( " " ) ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM/dd/uuuu" ) ;
List<LocalDate> dates = new ArrayList<>() ;
for( String element : elements ) {
try {
LocalDate ld = LocalDate.parse( element , f ) ;
dates.add( ld ) ;
} catch ( DateTimeParseException e ) {
// Ignore the exception. Move on to next element.
}
}
System.out.println( "dates: " + dates ) ;
請參閱IdeOne.com上的此代碼 。
日期:[2009-11-25,2009-11-30]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.