public static Map<String, String> getWhereConditions(String sql) {\n Map<String, String> whereMap = new HashMap<>();\n String regex = "WHERE\s+(.+)";\n Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);\n Matcher matcher = pattern.matcher(sql);\n\n if (matcher.find()) {\n String conditions = matcher.group(1);\n String[] pairs = conditions.split("\s+AND\s+");\n\n for (String pair : pairs) {\n String[] keyValue = pair.split("\s*=\s*");\n String key = keyValue[0];\n String value = keyValue[1].replaceAll("'", "");\n whereMap.put(key, value);\n }\n }\n\n return whereMap;\n}\n您可以使用 Pattern.CASE_INSENSITIVE 标志来忽略大小写匹配。在编译正则表达式模式时,将该标志作为第二个参数传递给 Pattern.compile() 方法。以下是修改后的代码示例:\n\njava\npublic static Map<String, String> getWhereConditions(String sql) {\n Map<String, String> whereMap = new HashMap<>();\n String regex = "WHERE\s+(.+)";\n Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);\n Matcher matcher = pattern.matcher(sql);\n\n if (matcher.find()) {\n String conditions = matcher.group(1);\n String[] pairs = conditions.split("\\s+AND\\s+");\n\n for (String pair : pairs) {\n String[] keyValue = pair.split("\\s*=\\s*");\n String key = keyValue[0];\n String value = keyValue[1].replaceAll("'", "");\n whereMap.put(key, value);\n }\n }\n\n return whereMap;\n}\n\n\n现在,无论您的 WHERE 子句是大写还是小写,都将匹配成功。

Java 正则表达式忽略大小写匹配 WHERE 子句

原文地址: http://www.cveoy.top/t/topic/qajK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录