输入:
"MyPrefix_CH-DE_ProductName.pdf"
期望的输出:
["MyPrefix", "CH", "DE", "ProductName"]
CH
是国家/地区代码,它应该来自预定义的列表,例如。 [“CH”、“IT”、“FR”、“GB”]
编辑:前缀也可以包含 _ 和 -,但不能包含 CH 或 DE。
DE
是一种语言代码,它应该来自预定义的列表,例如。 [“EN”、“IT”、“FR”、“DE”]
我该怎么做?
我正在寻找基于正则表达式的解决方案。
最佳答案
我假设扩展名始终是 pdf
String str = "MyPref_ix__CH-DE_ProductName.pdf";
String regex = "(.*)_(CH|IT|FR|GB)-(EN|IT|FR|DE)_(.*)\\.pdf";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
String[] res = new String[4];
if(matcher.matches()) {
res[0] = matcher.group(1);
res[1] = matcher.group(2);
res[2] = matcher.group(3);
res[3] = matcher.group(4);
}
关于java - 将文件名分成组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59067526/