java正則表達式匹配所有數(shù)字的案例
用于匹配的正則表達式為 :([1-9]\d*\.?\d*)|(0\.\d*[1-9])
(
[1-9] :匹配1~9的數(shù)字;
\d :匹配數(shù)字,包括0~9;
* :緊跟在 \d 之后,表明可以匹配零個及多個數(shù)字;
\. :匹配小數(shù)點;
? :緊跟在 \. 之后,表明可以匹配零個或一個小數(shù)點;
0 :匹配一個數(shù)字0;
)
其中的 [1-9]\d*\.?\d* 用以匹配諸如:1、23、34.0、56.78 之類的非負(fù)的整數(shù)和浮點數(shù);
其中的 0\.\d*[1-9] 用以匹配諸如:0.1、0.23、0.405 之類的非負(fù)浮點數(shù);
private List GetTmpFieldsList(List FieldsList,String tmptableName,String IndexName) { List maps = new ArrayList<>(); for(String field :FieldsList){ //必須包含傳入的標(biāo)識符,同時要包含數(shù)字 if(field.toLowerCase().contains(tmptableName.toLowerCase())){ FieldList e = new FieldList(); String [] fieldArray = field.split("\\.");//帶數(shù)字的string field = field.replaceAll("\\_?\\d+", ""); //去掉下劃線加數(shù)字 得有效的物理名 String [] fieldArray2 = field.split("\\."); //不帶數(shù)字的string Pattern p = Pattern.compile("\\d+"); //得到字符串中的數(shù)字 Matcher m = p.matcher(fieldArray[1]); if(m.find()){ int key = Integer.parseInt(m.group()); e.setCaseValue(key); if(StringUtils.isEqual(fieldArray2[1], IndexName)){ //for BAT203 e.setField("CHECK_POSITION"); //項目物理名 }else{ e.setField(fieldArray2[1]); //項目物理名 } e.setFieldName(fieldArray[1]); //項目物理名別名 maps.add(e); } /**else{ 只有后面帶數(shù)字的才可以 if(StringUtils.isEqual(fieldArray2[1],IndexName)){ //for BAT203 e.setField("CHECK_POSITION"); //項目物理名 }else{ e.setField(fieldArray2[1]); } e.setFieldName(fieldArray[1]); maps.add(e); }**/ } } //Add ACE商品マスタ.更新フラグ return maps; }
補充知識:關(guān)于fasterxml-jackson發(fā)生Can not deserialize instance of異常原因驗證
這兩天線上有大量的
java.lang.IllegalArgumentException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: N/A; line: -1, column: -1]錯誤發(fā)生。
有經(jīng)驗的人一看,就知道是對象屬性轉(zhuǎn)換發(fā)生異常了。為了把這個錯誤的根本原因找到。
只能上代碼模擬了。
/** * Created by changle on 17/1/9. */ @Slf4j public class JSONTest { public static void main(String[] args) { testAtoB(); //testAtoB() 發(fā)生:Can not deserialize instance of com.test.JSONTest$Hobby out of START_ARRAY token testBtoA(); //testBtoA() 發(fā)生:Can not deserialize instance of java.util.ArrayList out of START_OBJECT token } public static void testAtoB(){ List<Hobby> hobbies = new ArrayList<>(); Random random = new Random(); for(int i=0;i<3;i++){ Hobby hobby = new Hobby(random.nextInt(),"測試名稱","測試類型",random.nextInt(100)); hobbies.add(hobby); } StudentA studentA = new StudentA(); studentA.setAge(23); studentA.setFromCity(true); studentA.setMoney(3000); studentA.setName("張三"); studentA.setHobbies(hobbies); try { String str = JSON.json(studentA); log.info("str={}",str); //list轉(zhuǎn)換單個projo StudentB studentB = JsonUtil.jsonObject(str, StudentB.class); log.info("studentB.name:{}",studentB.getName()); } catch (Exception e) { e.printStackTrace(); } } public static void testBtoA(){ Random random = new Random(); Hobby hobby = new Hobby(random.nextInt(), "測試名稱", "測試類型", random.nextInt(100)); StudentB studentB2 = new StudentB(); studentB2.setAge(23); studentB2.setFromCity(true); studentB2.setMoney(3000); studentB2.setName("張三"); studentB2.setHobbies(hobby); String str2 = null; try { str2 = JSON.json(studentB2); //單個projo轉(zhuǎn)換list StudentA studentA2 = JsonUtil.jsonObject(str2, StudentA.class); log.info("studentB.name:{}", studentA2 == null ? "" : studentA2.getName()); } catch (IOException e) { e.printStackTrace(); } } @Data public static class StudentA{ private String name; private int age; private long money; private boolean isFromCity; private List<Hobby> hobbies; } @Data public static class StudentB{ private String name; private int age; private long money; private boolean isFromCity; private Hobby hobbies; } @Data public static class Hobby{ private long hId; private String hName; private String type; private int score; public Hobby(){} public Hobby(long hId,String hName,String type,int score){ this.hId = hId; this.hName = hName; this.type = type; this.score = score; } } }
結(jié)論:
Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
該錯誤是因為目標(biāo)類屬性keyX需要ArrayList類型的,待轉(zhuǎn)換的json串里屬性名keyX對應(yīng)的,不是一個ArrayList集合,而是單個 POJO。
Can not deserialize instance of com.test.JSONTest$Hobby out of START_ARRAY token
該錯誤是因為目標(biāo)類屬性keyX需要JSONTest$Hobby類型的,待轉(zhuǎn)換的json串里屬性名keyX對應(yīng)的,不是一個POJO對象,而是ArrayList集合。
以上這篇java正則表達式匹配所有數(shù)字的案例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用jquery 的ajax 與 Java servlet的交互代碼實例
這篇文章主要介紹了使用jquery 的ajax 與 Java servlet的交互代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09Dubbo在Spring和Spring Boot中的使用詳解
這篇文章主要介紹了Dubbo在Spring和Spring Boot中的使用詳解,需要的朋友可以參考下2017-10-10Intellij?IDEA根據(jù)maven依賴名查找它是哪個pom.xml引入的(圖文詳解)
這篇文章主要介紹了Intellij?IDEA根據(jù)maven依賴名查找它是哪個pom.xml引入的,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08快速搭建Spring Boot+MyBatis的項目IDEA(附源碼下載)
這篇文章主要介紹了快速搭建Spring Boot+MyBatis的項目IDEA(附源碼下載),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12