具有 URL 编码字符串的正则表达式

Regular expression with URL Encoded Strings

我有包含 URL 编码 (%22) 和其他字符 [!@#$%^&*] 的字符串。我需要使用 RegEx 检查字符串是否包含该组中的字符,但不包括 URL 编码引号 (%22)。我无法让负面展望正常工作,也无法让排除的字符串(或否定)工作。有人可以帮忙吗?到目前为止,这是不起作用的代码:

Pattern p = Pattern.compile("[!@#$%^&*]"); //
String[] tokens = {"%22Hobo%22", "Shoe*", "Rail%6Road","Sugar"};
for (String string : tokens) {
  Matcher m = p.matcher(string);
  boolean b = m.find()
  System.out.println(string + ": " + b);
}

所需的输出应该是 false、true、true、false。

(?!%22)[!@#$%^&*]

尝试 this.See 演示。

https://regex101.com/r/mS3tQ7/16

export const uriParser = (x) =>
  //replace/regex exclude-negated [set-of-tokens], doesn't work/parse for (%[A-Fa-f0-9]{2})+
  //decodeURI() does the same I believe, but this will always return a string,
  //without an error object
  //a-z or A-Z includes underscore '_' but not space whitespace, nor (\r\n|\r|\n)+
  x.replace(/(%[A-Fa-f0-9]{2})+[^a-zA-Z0-9-+ ]+/g, "_");

https://www.ietf.org/rfc/rfc3986.txt#:~:text=2.4.%20%20When%20to%20Encode%20or%20Decode%0A

为了我的目的,我让我的 uri link 碎片通过 (%[A-Fa-f0-9]{2})+ 安装,所以我使用 .replace("_"," ") 用于 ui 但 uriParser() 用于传出 links 在 ux 中尽可能绕过冗余。用例选择是在始终获取字符串和在此之前放置其他字符的规范之间。 “你为什么不使用 URLEncoder?” – Jens 对问题的评论