首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用replaceAll()替换部分匹配模式

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

Stack Overflow用户
提问于 2019-03-01 04:26:29
回答 1查看 55关注 0票数 0

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

代码语言:javascript
复制
\(?|&\)[^=]+=([^&]+)

如何使用替换url中查询的所有值。

还是在这里使用其他技巧?

案例-1

代码语言:javascript
复制
Actual: https://stackoverflow.com/questions/54937940?a=5
Expected: https://stackoverflow.com/questions/54937940?a=XX

案例-2

代码语言:javascript
复制
Actual: https://stackoverflow.com/questions/54937940?a=5&b=7
Expected: https://stackoverflow.com/questions/54937940?a=xx&b=xx

案例-3

代码语言:javascript
复制
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"

(如有任何帮助,将不胜感激:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-01 04:40:25

由于您只想匹配params的值,所以可以使用这个正则表达式来匹配这些值,并将它们替换为xx或您想要的任何东西。

代码语言:javascript
复制
(?<=[&=])[^=&"\n ]*(?=[&" ]|$)

演示

看看这个Java代码,

代码语言:javascript
复制
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"));
    });

指纹,

代码语言:javascript
复制
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"
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54937940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档