像这样的正则表达式模式:
".*/.*/.*/.*/.*/.*/(.*)-\d{2}\.\d{2}\.\d{2}.\d{4}.*"
真的很难维护。
我想知道,是否有这样的东西:
".*<userName>/.*<envName>/.*<serviceName>/.*<dataType>/.*<date>/.*<host>/(.*)-\d{2}\.\d{2}\.\d{2}.\d{4}.*<fileName>"
帮助更轻松地阅读/理解正则表达式?
更新于 2018 年 12 月 7 日
感谢@Liinux 的帮助,它被称为free-spacing,一个简单的java演示是:
public static void main(String[] args) {
String re = "(?x)"
+ "# (?x) is the free-spacing flag\n"
+ "#anything here between the first and last will be ignored\n"
+ "#in free-spacing mode, whitespace between regular expression tokens is ignored\n"
+ "(19|20\\d\\d) # year (group 1)\n"
+ "[-/\\.] # separator\n"
+ "(\\d{2}) # month (group 2)\n"
+ "[-/\\.] # separator\n"
+ "(\\d{2}) # day (group 3)";
Pattern pattern = Pattern.compile(re);
Stream.of("2018-12-07", "2018.12.07", "2018/12/07").forEach(aTest -> {
System.out.println("**************** Testing: " + aTest);
final Matcher matcher = pattern.matcher(aTest);
if (matcher.find()) {
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group - " + i + ": " + matcher.group(i));
}
}
});
}