实例:自动化脚本关键字扫描
public static void main(String[] args) {
String script = "echo \"hello world\"\n" +
"rm -rf ./*\n" +
"reboot";
String keyWord = "rm -rf";
String scriptStr = script.replaceAll("\\s"," ");
if(keyWord.contains(" ")){
System.out.println(scriptStr.contains(keyWord));
}else{
String rex = ".*\\b"+keyWord+"\\b.*";
Pattern pattern = Pattern.compile(rex);
Matcher matcher =pattern.matcher(scriptStr);
System.out.println(matcher.matches());
}
- 是否包含某个单词
.*\\breboot\\b.*