我得到了一个杂志名称列表,其中可能包含一个或多个周数。
例如:
足球国际wk43
国家地理(wk50)
学校wk39 / wk43
一些杂志12周到16年另一个 杂志wk36_38
另一本杂志wk36_wk38
等。
我想要的是将最后一部分作为一周。
所以:
足球国际周43周年国家地理周50周年 学校论文第39周 - 第43周一些杂志第12周 - 第16周 另一本杂志周36周 - 周38另一本杂志周36周 38
我开始时:
Pattern pat = Pattern.compile("(wk|week)[\\(\\_]?([0-9]{1,2}\\-?[0-9]{0,2})");
但这不起作用:
(some wk36 tm 42)", "(some wk36/wk37)", "(some wk36_wk37)", "some wk36_37", "some wk36_wk37"
我尝试做以下事项:
阅读第一次出现的周或周(周|周),然后获取所有内容。
用周替换每周发生的事情
以某种方式替换所有非数字字符(例如/_-).
答案 0 :(得分:4)
您可以将Matcher#appendReplacement
与以下正则表达式一起使用:
(?i)w(?:e{2})?k(\\d+)(?:(?:\\s*until\\s*|[ _\\/])(?:w(?:e{2})?k)?(\\d+))?
以下是code demo:
String rx = "(?i)w(?:e{2})?k(\\d+)(?:(?:\\s*until\\s*|[ _\\/])(?:w(?:e{2})?k)?(\\d+))?";
String s = "Soccer International wk43\nNational Geopgraphic (wk50)\nSchoolpaper wk39/wk43\nSome magazine week12 until 16\nAnother magazine wk36_38\nAnother magazine wk36_wk38";
StringBuffer result = new StringBuffer();
Matcher m = Pattern.compile(rx).matcher(s);
while (m.find()) {
String replacement = m.group(2) == null ? // Check if Group 2 is matched
"week " + m.group(1): // If not, use just Group 1
"week " + m.group(1) + " - week " + m.group(2); // If yes, Group 2 is added
m.appendReplacement(result, replacement); // Add the replacement
}
m.appendTail(result);
System.out.println(result.toString());
更复杂场景的更新:
String rx = "(?i)w(?:e{2})?k\\s*(\\d+)(?: +(\\d{4})\\b)?(?:(?:\\s*(?:until|tm)\\s*|[ _/])(?:w(?:e{2})?k)?(\\d+)(?: +(\\d{4})\\b)?)?";
String s = "wk 1 2016\n(wk 47 2015 tm 9 2016)\nSoccer International wk43\nNational Geopgraphic (wk50)\nSchoolpaper wk39/wk43\nSome magazine week12 until 16\nAnother magazine wk36_38\nAnother magazine wk36_wk38";
StringBuffer result = new StringBuffer(); // week 47 (2015) - week 9 (2016)
Matcher m = Pattern.compile(rx).matcher(s); // week 1 (2016)
while (m.find()) {
String replacement = "";
String prt1 = ""; String prt2 = "";
if (m.group(2) != null) {
prt1 += " (" + m.group(2) + ")";
}
if (m.group(4) != null) {
prt2 += " (" + m.group(4) + ")";
}
if (m.group(3) == null) {
replacement = "week " + m.group(1) + prt1;
} else {
replacement = "week " + m.group(1) + prt1 + " - week " + m.group(3) + prt2;
}
m.appendReplacement(result, replacement);
}
m.appendTail(result);
System.out.println(result.toString());