質問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)
何故ですか?