一、前言
通过jena-sdb源码包(1.5.1)中com.hp.hpl.jena.sdb.util.RegexUtils正则规则工具类,对指定位置字符串判断是否包含特殊字符noMetaChars、SQL指定特殊字字符替换为%统配字符like匹配等方法处理。
二、源码说明
package com.hp.hpl.jena.sdb.util;@b@@b@import java.text.StringCharacterIterator;@b@@b@public class RegexUtils@b@{@b@ static final char[] metaChars = { '^', '$', '.', '*', '?', '+', '(', ')', '[', ']', '\\' };@b@@b@ public static boolean noMetaChars(String string, int fromIndex)@b@ {@b@ return noMetaChars(string, fromIndex, string.length());@b@ }@b@@b@ public static boolean noMetaChars(String string, int fromIndex, int endIndex)@b@ {@b@ char ch;@b@ char[] arr$;@b@ int i$;@b@ StringCharacterIterator iter = new StringCharacterIterator(string, fromIndex, endIndex, fromIndex);@b@@b@ while ((ch = iter.next()) != 65535)@b@ {@b@ arr$ = metaChars; int len$ = arr$.length; for (i$ = 0; i$ < len$; ++i$) { char mc = arr$[i$];@b@@b@ if (mc == ch)@b@ return false;@b@ }@b@ }@b@ return true;@b@ }@b@@b@ public static boolean isSimpleStartsWith(String pattern)@b@ {@b@ if (pattern.length() < 1)@b@ return false;@b@ return ((pattern.charAt(0) == '^') && (noMetaChars(pattern, 1)));@b@ }@b@@b@ public static boolean isSimpleAnchored(String pattern)@b@ {@b@ if (pattern.length() < 2)@b@ return false;@b@@b@ return ((pattern.charAt(0) == '^') && (pattern.charAt(pattern.length() - 1) == '$') && (noMetaChars(pattern, 1, pattern.length() - 1)));@b@ }@b@@b@ public static boolean isSimpleEndsWith(String pattern)@b@ {@b@ if (pattern.length() < 1)@b@ return false;@b@@b@ return ((pattern.charAt(pattern.length() - 1) == '$') && (noMetaChars(pattern, 0, pattern.length() - 1)));@b@ }@b@@b@ public static String regexToLike(String pattern)@b@ {@b@ if (isSimpleAnchored(pattern))@b@ return pattern.substring(1, pattern.length() - 1);@b@@b@ if (isSimpleStartsWith(pattern))@b@ return pattern.substring(1) + "%";@b@@b@ if (isSimpleEndsWith(pattern))@b@ return "%" + pattern.substring(0, pattern.length() - 1);@b@@b@ if (noMetaChars(pattern, 0))@b@ return "%" + pattern + "%";@b@@b@ return null;@b@ }@b@}