java正則表達式匹配所有數字的案例
用于匹配的正則表達式為 :([1-9]d*.?d*)|(0.d*[1-9])
(
[1-9] :匹配1~9的數字;
d :匹配數字,包括0~9;
* :緊跟在 d 之后,表明可以匹配零個及多個數字;
. :匹配小數點;
? :緊跟在 . 之后,表明可以匹配零個或一個小數點;
0 :匹配一個數字0;
)
其中的 [1-9]d*.?d* 用以匹配諸如:1、23、34.0、56.78 之類的非負的整數和浮點數;
其中的 0.d*[1-9] 用以匹配諸如:0.1、0.23、0.405 之類的非負浮點數;
private List GetTmpFieldsList(List FieldsList,String tmptableName,String IndexName) { List maps = new ArrayList<>(); for(String field :FieldsList){ //必須包含傳入的標識符,同時要包含數字 if(field.toLowerCase().contains(tmptableName.toLowerCase())){ FieldList e = new FieldList(); String [] fieldArray = field.split('.');//帶數字的string field = field.replaceAll('_?d+', ''); //去掉下劃線加數字 得有效的物理名 String [] fieldArray2 = field.split('.'); //不帶數字的string Pattern p = Pattern.compile('d+'); //得到字符串中的數字 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]); //?目物理名?e名 maps.add(e); } /**else{ 只有后面帶數字的才可以 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; }
補充知識:關于fasterxml-jackson發生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]錯誤發生。
有經驗的人一看,就知道是對象屬性轉換發生異常了。為了把這個錯誤的根本原因找到。
只能上代碼模擬了。
/** * Created by changle on 17/1/9. */@Slf4jpublic class JSONTest { public static void main(String[] args) { testAtoB(); //testAtoB() 發生:Can not deserialize instance of com.test.JSONTest$Hobby out of START_ARRAY token testBtoA(); //testBtoA() 發生: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轉換單個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轉換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; } }}
結論:
Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
該錯誤是因為目標類屬性keyX需要ArrayList類型的,待轉換的json串里屬性名keyX對應的,不是一個ArrayList集合,而是單個 POJO。
Can not deserialize instance of com.test.JSONTest$Hobby out of START_ARRAY token
該錯誤是因為目標類屬性keyX需要JSONTest$Hobby類型的,待轉換的json串里屬性名keyX對應的,不是一個POJO對象,而是ArrayList集合。
以上這篇java正則表達式匹配所有數字的案例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章:
