如何使用 Java 正则表达式替换 URL 中的所有特殊字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7046915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:27:14  来源:igfitidea点击:

How can I replace all special characters in a URL, using a Java regular expression?

javaregex

提问by Shamik

I'm trying use regular expression to convert special characters in an url. Here's my sample code :

我正在尝试使用正则表达式来转换 url 中的特殊字符。这是我的示例代码:

String formatUrl = "index.php?title=Test/enu/test/Tips_%26_Tricks/Tips_and_Tricks";
formatUrl = formatUrl.replaceAll("[^a-zA-Z0-9]" , "-");

What I'm trying to do is to convert the special characters in the url such as ?_%. to "-" excluding "/".

我想要做的是转换 url 中的特殊字符,例如 ?_%。到“-”,不包括“/”。

The regular expression in my code converts everything resulting the output as

我的代码中的正则表达式将导致输出的所有内容转换为

index-php-title-Test-enu-test-Tips--26-Tricks-Tips-and-Tricks

But I want it to be

但我希望它是

index-php-title-Test/enu/test/Tips--26-Tricks/Tips-and-Tricks

Any pointers will be appreciated.

任何指针将不胜感激。

回答by Paul

You could just add your /into the regex:

您可以将您/的添加到正则表达式中:

"[^a-zA-Z0-9/]"

回答by Jonathan M

formatUrl = formatUrl.replaceAll("[^a-zA-Z0-9/]" , "-");

回答by hoipolloi

I'm wondering what you're trying to achieve. Why not just decode the URL?

我想知道你想达到什么目的。为什么不只解码 URL?

final String url = "index.php?title=Test/enu/test/Tips_%26_Tricks/Tips_and_Tricks";
final String decoded = java.net.URLDecoder.decode(url, "UTF-8");
System.out.println(decoded); // Prints index.php?title=Test/enu/test/Tips_&_Tricks/Tips_and_Tricks