@Test public void buildSQLPatternTest() { String[][] testValues = { { "foo", "foo", "fool" }, { "%here%", "111here111", "her1e" }, { "h%re%", "h111rexxx", "1here" }, { "\\%foo", "%foo", "here" }, { "__foo", "22foo", "1foo" }, { "%\\_%", "has_underscore", "nouscore" }, { "???foo", "???foo", "123foo" } }; String msg; for (int i = 0; i < testValues.length; i++) { msg = "Item " + i + " failed"; Pattern p = PEStringUtils.buildSQLPattern(testValues[i][0]); assertTrue(msg, p.matcher(testValues[i][1]).matches()); assertFalse(msg, p.matcher(testValues[i][2]).matches()); } }
public static Pattern buildSQLPattern(String likeExpr) { char[] sqlPattern = likeExpr.trim().toLowerCase().toCharArray(); StringBuffer javaPattern = new StringBuffer(); boolean escape = false; for(int i = 0; i < sqlPattern.length; i++) { if (escape) { javaPattern.append(sqlPattern[i]); escape = false; } else if (sqlPattern[i] == '\\') escape = true; else if (".?+*|[]{}()^$-".indexOf(sqlPattern[i]) > -1) { javaPattern.append("\\"); javaPattern.append(sqlPattern[i]); } else if (sqlPattern[i] == '%') javaPattern.append(".*"); else if (sqlPattern[i] == '_') javaPattern.append("."); else javaPattern.append(sqlPattern[i]); } return Pattern.compile(javaPattern.toString()); }
PEStringUtils { public static Pattern buildSQLPattern(String likeExpr) { char[] sqlPattern = likeExpr.trim().toLowerCase().toCharArray(); StringBuffer javaPattern = new StringBuffer(); boolean escape = false; for(int i = 0; i < sqlPattern.length; i++) { if (escape) { javaPattern.append(sqlPattern[i]); escape = false; } else if (sqlPattern[i] == '\\') escape = true; else if (".?+*|[]{}()^$-".indexOf(sqlPattern[i]) > -1) { javaPattern.append("\\"); javaPattern.append(sqlPattern[i]); } else if (sqlPattern[i] == '%') javaPattern.append(".*"); else if (sqlPattern[i] == '_') javaPattern.append("."); else javaPattern.append(sqlPattern[i]); } return Pattern.compile(javaPattern.toString()); } }
PEStringUtils { public static Pattern buildSQLPattern(String likeExpr) { char[] sqlPattern = likeExpr.trim().toLowerCase().toCharArray(); StringBuffer javaPattern = new StringBuffer(); boolean escape = false; for(int i = 0; i < sqlPattern.length; i++) { if (escape) { javaPattern.append(sqlPattern[i]); escape = false; } else if (sqlPattern[i] == '\\') escape = true; else if (".?+*|[]{}()^$-".indexOf(sqlPattern[i]) > -1) { javaPattern.append("\\"); javaPattern.append(sqlPattern[i]); } else if (sqlPattern[i] == '%') javaPattern.append(".*"); else if (sqlPattern[i] == '_') javaPattern.append("."); else javaPattern.append(sqlPattern[i]); } return Pattern.compile(javaPattern.toString()); } private  PEStringUtils(); }
PEStringUtils { public static Pattern buildSQLPattern(String likeExpr) { char[] sqlPattern = likeExpr.trim().toLowerCase().toCharArray(); StringBuffer javaPattern = new StringBuffer(); boolean escape = false; for(int i = 0; i < sqlPattern.length; i++) { if (escape) { javaPattern.append(sqlPattern[i]); escape = false; } else if (sqlPattern[i] == '\\') escape = true; else if (".?+*|[]{}()^$-".indexOf(sqlPattern[i]) > -1) { javaPattern.append("\\"); javaPattern.append(sqlPattern[i]); } else if (sqlPattern[i] == '%') javaPattern.append(".*"); else if (sqlPattern[i] == '_') javaPattern.append("."); else javaPattern.append(sqlPattern[i]); } return Pattern.compile(javaPattern.toString()); } private  PEStringUtils(); static String buildPath(String dir, String sep, String name); static StringBuffer toString(StringBuffer sb, String name, Collection<? extends Object> values); static StringBuffer toArrayString(StringBuffer sb, Collection<? extends Object> values); static String toString(String name, Collection<? extends Object> values); static String toString(Class<T> theClass, Collection<? extends Object> values); static StringBuffer toString(StringBuffer sb, String name, Map<K, V> values); static String toString(String name, Map<K, V> values); static String[] toArray(Collection<String> values); static String trimToInt(final String numeric); static String normalizePrefix(String prefix); static String toString(Throwable e); static Pattern buildSQLPattern(String likeExpr); static String singleQuote(final String value); static boolean isQuoted(final String value); static String dequote(final String in); static String escapeSingleQuoteIfNecessary(String t); static boolean toBoolean(String value); static boolean isHexNumber(final String value); static String getIndented(final String value); static String getIndented(final String value, final int numIndents); static String getIndented(final String value, final String indent, final int numIndents); static String buildIndent(final int numIndents); static String buildIndent(final String indent, final int numIndents); }
PEStringUtils { public static Pattern buildSQLPattern(String likeExpr) { char[] sqlPattern = likeExpr.trim().toLowerCase().toCharArray(); StringBuffer javaPattern = new StringBuffer(); boolean escape = false; for(int i = 0; i < sqlPattern.length; i++) { if (escape) { javaPattern.append(sqlPattern[i]); escape = false; } else if (sqlPattern[i] == '\\') escape = true; else if (".?+*|[]{}()^$-".indexOf(sqlPattern[i]) > -1) { javaPattern.append("\\"); javaPattern.append(sqlPattern[i]); } else if (sqlPattern[i] == '%') javaPattern.append(".*"); else if (sqlPattern[i] == '_') javaPattern.append("."); else javaPattern.append(sqlPattern[i]); } return Pattern.compile(javaPattern.toString()); } private  PEStringUtils(); static String buildPath(String dir, String sep, String name); static StringBuffer toString(StringBuffer sb, String name, Collection<? extends Object> values); static StringBuffer toArrayString(StringBuffer sb, Collection<? extends Object> values); static String toString(String name, Collection<? extends Object> values); static String toString(Class<T> theClass, Collection<? extends Object> values); static StringBuffer toString(StringBuffer sb, String name, Map<K, V> values); static String toString(String name, Map<K, V> values); static String[] toArray(Collection<String> values); static String trimToInt(final String numeric); static String normalizePrefix(String prefix); static String toString(Throwable e); static Pattern buildSQLPattern(String likeExpr); static String singleQuote(final String value); static boolean isQuoted(final String value); static String dequote(final String in); static String escapeSingleQuoteIfNecessary(String t); static boolean toBoolean(String value); static boolean isHexNumber(final String value); static String getIndented(final String value); static String getIndented(final String value, final int numIndents); static String getIndented(final String value, final String indent, final int numIndents); static String buildIndent(final int numIndents); static String buildIndent(final String indent, final int numIndents); static final String FILE_SEP; }