简体   繁体   English

寻找解决方案[Java,Regex?]

[英]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.

相关问题 Java的正则表达式懒惰解决方案? - Regex lazy solution for java? Java 8 Stream是我正在寻找的解决方案吗? - Is Java 8 Stream the solution I'm looking for? 寻找解决方案以获取基于Java中Key的数量 - Looking for solution to get a count of based on the Key in Java 在 Java 中寻找性能更好的字符串替换解决方案 - Looking for a better performant String replace solution in Java 用于解析此字符串的正则表达式或java解决方案 - Regex or java solution for parsing this string 寻找一个正则表达式来匹配 Java 中的特定算术字符串 - Looking for a regex to match a specific arithmetic String in Java 为AngularJS客户端寻找App Engine Channel Api Java替代解决方案 - Looking for App Engine Channel Api Java replacement solution for AngularJS client 我正在寻找一种解决Java过度冗长而不使用Groovy的方法 - I'm looking for a solution to the excessive verboseness of Java without using Groovy 使用 Java 并发 API 寻找干净的事件同步解决方案 - Looking for clean solution for event-synchronization with Java Concurrency API Android Studio Java - 在通过的活动中寻找重置先前活动的解决方案 - Android Studio Java - looking solution to reset Previous Activity in passed activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM