How can I replace all special characters in a URL, using a Java regular expression?
I'm trying use regular expression to convert special characters in an url. Here's my sample code :
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 "/".
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.
You could just add your /
into the regex:
"[^a-zA-Z0-9/]"
formatUrl = formatUrl.replaceAll("[^a-zA-Z0-9/]" , "-");
I'm wondering what you're trying to achieve. Why not just decode the 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
精彩评论