此模式匹配一个等号,后跟一个可选空格和一个用 @ 符号括起来的单词:
Pattern pattern = Pattern.compile("= ?@(.*)@");
Matcher matcher = pattern.matcher("=@WORD@");
if (matcher.matches()) {
System.out.println(matcher.group(1));
}
// Prints: "WORD"
除非我误解了您希望完成的任务,否则看不到需要向后看。但是,以下应该有效:
Pattern pattern = Pattern.compile("(?<== ?)@(.*)@");
Matcher matcher = pattern.matcher("= @WORD@");
if (matcher.find()) {
System.out.println(matcher.group(1));
}
// Prints: "WORD"
然后按如下方式完成否定模式:
Pattern pattern = Pattern.compile("(?<!= ?)@(.*)@");
Matcher matcher = pattern.matcher("=@WORD@");
System.out.println(matcher.find());
// Prints: "false"