gpt4 book ai didi

Java 正则表达式替换 : and/except the domain name in the url to white space

转载 作者:行者123 更新时间:2023-11-29 07:32:19 30 4
gpt4 key购买 nike

我有一个很长的字符串,包括很多 :/ .它还包括 url。

我想替换所有:/但 url 的域名(例如 http://example.com)为空白。

所以 link:http://example.com/test/page.html将变为 link http://example.com test page.html .

我试过了 replaceAll("[://]", " ")但它也取代了 :/http://example.com到空白。

最佳答案

现在看起来你可能想要使用类似的东西:

url = url.replaceAll("(https?://[^/:]+)?[/:]", "$1 ")

$1 表示来自组 1 (https?://[^/:]+) 的匹配项,这要感谢 ? 是可选的。

因此它将尝试找到任何 /: 并将其替换为空格。如果在任何这些字符之前有 http://address 部分,它将被自己替换。

关于Java 正则表达式替换 : and/except the domain name in the url to white space,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40453004/

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