java正则表达式匹配所有数字的案例
作者:skyxmstar 时间:2023-07-07 02:59:38
用于匹配的正则表达式为 :([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]); //項目物理名別名
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.
*/
@Slf4j
public 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集合。
来源:https://blog.csdn.net/skyxmstar/article/details/66975983