可以使用以下正则表达式来匹配该模式:
\b\w+\b\s*(!=|==)\s*\b\w+\b(\s*!\s*\b\w+\b)*
示例代码:
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
String text = "word1 != word2 ! word3 == word4 ! word5";
String pattern = "\\b\\w+\\b\\s*(!=|==)\\s*\\b\\w+\\b(\\s*!\\s*\\b\\w+\\b)*";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(text);
if (m.find()) {
System.out.println("匹配成功!" + m.group(0));
} else {
System.out.println("未找到匹配的模式!");
}
}
}