Reputation: 1731
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.
Upvotes: 2
Views: 14583
Reputation: 8044
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
Upvotes: 2