gpt4 book ai didi

java - 使用replaceAll()替换部分匹配模式

转载 作者:行者123 更新时间:2023-12-01 19:42:19 25 4
gpt4 key购买 nike

捕获 url 中的查询值的有效模式可能是

\(?|&\)[^=]+=([^&]+)

如何使用 replaceAll 替换网址中查询的所有

或者在这里使用其他技巧?

案例 - 1

Actual: https://stackoverflow.com/questions/54937940?a=5
Expected: https://stackoverflow.com/questions/54937940?a=XX

案例 - 2

Actual: https://stackoverflow.com/questions/54937940?a=5&b=7
Expected: https://stackoverflow.com/questions/54937940?a=xx&b=xx

案例 - 3

Actual: *266 open() "/usr/local/nginx/html/user-accounts/malphas/check" failed (2: No such file or directory), client: 10.254.3.0, server: ji, request: "GET /user-accounts/malphas/check?sid=ExecAuthoritySetting HTTP/2.0", host: "jilcom", referrer: "https://jicom/user-accounts/authority/authoritysettings/authoritysetting/detail?sid=ExecAuthoritySetting&roleId=1812""
tid"
Expected: *266 open() "/usr/local/nginx/html/user-accounts/malphas/check" failed (2: No such file or directory), client: 10.254.3.0, server: ji, request: "GET /user-accounts/malphas/check?sid=xx HTTP/2.0", host: "jilcom", referrer: "https://jicom/user-accounts/authority/authoritysettings/authoritysetting/detail?sid=xx&roleId=xx""
tid"

任何帮助将不胜感激:)

最佳答案

由于您只想匹配参数的值,因此可以使用此正则表达式通过环顾四周来仅匹配值并将其替换为 xx 或您想要的任何内容。

(?<=[&=])[^=&"\n ]*(?=[&" ]|$)

<强> Demo

看看这个 Java 代码,

List<String> list = Arrays.asList("https://stackoverflow.com/questions/54937940?a=5",
"https://stackoverflow.com/questions/54937940?a=5&b=7", "*266 open() \"/usr/local/nginx/html/user-accounts/malphas/check\" failed (2: No such file or directory), client: 10.254.3.0, server: ji, request: \"GET /user-accounts/malphas/check?sid=ExecAuthoritySetting HTTP/2.0\", host: \"jilcom\", referrer: \"https://jicom/user-accounts/authority/authoritysettings/authoritysetting/detail?sid=ExecAuthoritySetting&roleId=1812\"\"tid\"");
list.forEach(x -> {
System.out.println(x + " --> " + x.replaceAll("(?<=[&=])[^=&\"\\n ]*(?=[&\" ]|$)", "xx"));
});

打印,

https://stackoverflow.com/questions/54937940?a=5 --> https://stackoverflow.com/questions/54937940?a=xx
https://stackoverflow.com/questions/54937940?a=5&b=7 --> https://stackoverflow.com/questions/54937940?a=xx&b=xx
*266 open() "/usr/local/nginx/html/user-accounts/malphas/check" failed (2: No such file or directory), client: 10.254.3.0, server: ji, request: "GET /user-accounts/malphas/check?sid=ExecAuthoritySetting HTTP/2.0", host: "jilcom", referrer: "https://jicom/user-accounts/authority/authoritysettings/authoritysetting/detail?sid=ExecAuthoritySetting&roleId=1812""tid" --> *266 open() "/usr/local/nginx/html/user-accounts/malphas/check" failed (2: No such file or directory), client: 10.254.3.0, server: ji, request: "GET /user-accounts/malphas/check?sid=xx HTTP/2.0", host: "jilcom", referrer: "https://jicom/user-accounts/authority/authoritysettings/authoritysetting/detail?sid=xx&roleId=xx""tid"

关于java - 使用replaceAll()替换部分匹配模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54937940/

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