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



我有一个 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 具有上述任何运算符,则它应该打印"有一个运算符">

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

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

从模式中删除^,您的逻辑应该可以工作

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

Pattern pp = Pattern.compile("[$,=?@#'^<>.()%!]");
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);
}
if (Patern.matches("^[$,=?@#'<>.()%!]",urlParameters)) {
System.out.println("There is an Operator");
}

最新更新