package com.ume.controller;
/**
* 大小写敏感
* <p>
* ^x: 以x开头
* $x: 以x结尾
* !x: 与x不相等
* x: 与x相等
* @x: 包含x
* ~x: 不包含x
* x为空: 全匹配
* x为其它字符: 不匹配(默认设置-)
* </p>
*/
public class CustomizeMatch {
public static void main(String[] args) {
String rs[] = "^1.4. ,^2. ,^V7. , !2.1, 3.x.x.x, $x.x, @1.x, @3., @.5, ~y., ~3.".replaceAll("\\s*", "").split("[,|,]");
String ss[] = {"1.4.x", "1.5.x", "2.x", "2.1.x", "3.x.x.x", "V7.3.x"};
for (String s : ss) {
for (String r : rs) {
System.out.println(s + " " + r + " " + match(r, s));
}
}
System.out.println(match("^2.","2.0"));
}
public static boolean matchIgnoreCase(String regex, String string) {
if (isEmpty(regex)){
return true;
}
if(isEmpty(string)) {
return false;
}
return match(regex.toLowerCase(), string.toLowerCase());
}
public static boolean match(String regex, String string) {
if (isEmpty(regex)){
return true;
}
if(isEmpty(string)) {
return false;
}
String rs[] = regex.replaceAll("\\s*", "").split("[,|,]");
for(String r: rs) {
boolean result;
char c = r.charAt(0);
switch (c) {
case '^':
result = string.startsWith(r.substring(1));
break;
case '$':
result = string.endsWith(r.substring(1));
break;
case '!':
result = !string.equals(r.substring(1));
break;
case '@':
result = string.contains(r.substring(1));
break;
case '~':
result = !string.contains(r.substring(1));
break;
default:
result = string.equals(r);
break;
}
if(result) return true;
}
return false;
}
private static boolean isEmpty(String s) {
return s == null || "".equals(s);
}
}