[英]Looking for solution [Java, Regex?]
I have the following usecase and looking for the right solution. 我有以下用例,正在寻找正确的解决方案。
We are reading a csv file and populating a POJO.One column in from the file could map to multiple fields in the POJO. 我们正在读取一个csv文件并填充一个POJO,文件中的一列可能会映射到POJO中的多个字段。
eg: 例如:
Class Test {
private String field1;
private String field2;
private String field3;
......
other fields
---
getter methods
.....
setter methods
}
Let us say value of column XXXX from file is 123_TEST_34_THJ 假设文件中XXXX列的值为123_TEST_34_THJ
the field1 value is 123 field2 value is 34 filed3 value THJ field1的值是123 field2的值是34字段3的值THJ
I can do this in the Java code, but I am looking a solution to split this dynamically. 我可以在Java代码中执行此操作,但我正在寻找一种解决方案以动态拆分此内容。 Let us say the value of XXXX is now changed to 124_34_THJ, I want my code to work just with a configuration change. 假设XXXX的值现在已更改为124_34_THJ,我希望我的代码仅在配置更改后即可使用。 What would be a better solution? 有什么更好的解决方案? Please let me know if the question is not clear. 如果问题不清楚,请告诉我。
You could use a regular expression with named captures: 您可以将正则表达式与命名捕获一起使用:
(?<field1>\d+)_\w+_(?<field2>\d+)_(?<field3>\w+)
Then next week it might be... 那么下周可能是...
(?<field2>\d+)_(?<field7>\w+)_(?<field1>\d+)_(?<field3>\w+)
More info on your choices for regex libraries with named captures at Regex Named Groups in Java 有关在Java中以正则表达式命名组命名的捕获的正则表达式库的更多选择信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.