问题 1。
String matchedKey = "sessions.0.something.else";
Pattern newP = Pattern.compile("sessions\\.([^\\.]+)(\\..+)");
m = newP.matcher(matchedKey);
System.out.println(m.group(1)); // has nothing. Why?
sessions\\. // word "sessions" followed by .
([^\\.]+) // followed by something that is not a literal . at least once
(\\..+) // followed by literal . and anything at least once
我本来希望 m.group(1) 为 0
问题2
String mask = "sessions.{env}";
String maskRegex = mask.replace(".", "\\\\.").replace("env", "(.+)")
.replace("{", "").replace("}", "");
// produces mask "sessions\\.(.+))"
当用作
Pattern newP = Pattern.compile("sessions\\.(.+))"); // matches matchedKey (above)
Pattern newP = Pattern.compile(maskRegex); // does not match matchedKey (above)
这是为什么?