[英]Regex to find the first occurence of first 8 digits from a string in Java
這是我的String 20161011 ,我想得到第一個String 20161011 。
我正在使用(^|\\\\s)([0-9]+)($|\\\\s)
,但是它不起作用,有人可以建議正確的用法嗎,我想檢索的第一個字符串是日期格式為yyyymmdd ,我不需要驗證日期格式,因為它是預先驗證的。
這應該給您您想要的:
^([0-9]{8}).*
正則表達式$(\\d{8})\\.
將對您的樣品起作用。 但是,您可能真的想按此答案中所述分割字符串。 這樣您就可以訪問每個號碼,而不僅僅是第一個。 它可能也快一點。
這是您可以實現的方法,
Pattern r = Pattern.compile("\\d{8}+");
Matcher m = r.matcher("12345678.231610.01234567");
String str = "";
if (m.find()) {
// Only stores first occurence, occuring at any index of string.
str = m.group();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.