gpt4 book ai didi

java - 模式匹配不适用于 JAVA 中的字符串

转载 作者:行者123 更新时间:2023-12-02 09:08:59 27 4
gpt4 key购买 nike

我有一个已解码的 HTTP 请求输入字符串,应检查它是否包含“[$,=?@#'<>.()%!]^”等运算符。

我用谷歌搜索并根据它们找到了一些工作代码,我已经使用运算符传递了值,但它不起作用:

String urlParameters = httpRequest.getQueryString();
try {
String prevURL="";
String decodeURL=urlParameters;
while(!prevURL.equals(decodeURL))
{
prevURL=decodeURL;
decodeURL=URLDecoder.decode( decodeURL, "UTF-8" );
}
urlParameters=decodeURL;
}
catch (Exception e) {
System.out.println("Exception on decoding:" + e);
}
Pattern pp = Pattern.compile("[$,=?@#'<>.()%!]^");
Matcher mm = pp.matcher(urlParameters);
if (mm.find()) {
System.out.println("There is an Operator");
}

如果 urlParameters 具有上述任何运算符,则应打印“There is an Operator”

最佳答案

您有^模式末尾的运算符 - "[$,=?@#'<>.()%!]^"

^ - 用于匹配字符串第一个字符之前的位置。

删除^根据你的模式和你的逻辑应该有效

如果^需要作为特殊字符集的一部分,请将其包含在 []

Pattern pp = Pattern.compile("[$,=?@#'^<>.()%!]");

关于java - 模式匹配不适用于 JAVA 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59561479/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com