正則法則提取文字裡的時間

2021-08-21 08:17:58 字數 1187 閱讀 4915

####1、功能概述

實現通過正則法則,來提取乙個文字裡面包含的時間

####2、實現思路

1)編寫適合篩選格式的正交法則

如圖實現 * 年 * 月 * 日的篩選

//****年**月**日   ****年*月*日 的格式,我們可以更改不同篩選規則,進行不同格式篩選

pattern = pattern.compile("((([0-9])年([0-9]|[1-9]))月([0-9]|[1-9]))日"); //嘗試提取這樣型別的資料

matcher = pattern.matcher(content);

system.out.println("查詢 年 月 日 ");

/**while (matcher.find())

*/if (matcher.find())

2)對符合篩選規格的資料進行擷取判斷,是否是否和年月日的日期規則

擷取年月日

year = integer.parseint(str_ymd.substring(0, str_ymd.lastindexof("年")));  //擷取年之前字串,並轉化 int

month = integer.parseint(str_ymd.substring(str_ymd.indexof("年") + 1, str_ymd.lastindexof("月"))); //擷取 年 月 之間字串,並轉化 int

day = integer.parseint(str_ymd.substring(str_ymd.indexof("月") + 1, str_ymd.lastindexof("日"))); //擷取 月 日 之間字串 , 並轉化 int

進行判斷:

//將篩選到的資料進行判斷,是否符合日期

private void judgedata(int year, int month,int day) else

if ((0同樣的道理,大家也可以,篩選 年月 , 時分秒 等不同格式和樣式的時間串,具有很好的延展性。

demo 例項

文獻參考:

時間日期的正則

金融文字時間提取

瀏覽github發現乙個有趣的庫,在裡面發現了乙個非常非常有用的庫 from coconlp.extractor import extractor ex extractor times ex.extract time text print times time記錄一下。順便感謝開源大佬!以及 尋人微...

Split 分段取文字裡的值

單個字元隔開 例項 頁面 body form id form1 runat server div div form body 後台 protected void page load object sender,eventargs e protected void button1 click obje...

正規表示式提取時間

時間的各種格式都可以通過正規表示式來匹配,例如我們想精確匹配hh mm的時間,即包含小時和分鐘,可以使用下面的表示式 0 9 0 0 9 1 0 9 2 0 3 0 5 0 9 0 9 0 0 9 1 0 9 2 0 3 0 5 0 9 更多關於時間和日期的正規表示式,參考 regexlib.pub...