Shamik
Shamik

Reputation: 1731

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.

Upvotes: 2

Views: 14583

Answers (3)

Paul
Paul

Reputation: 141877

You could just add your / into the regex:

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

Upvotes: 9

hoipolloi
hoipolloi

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

Jonathan M
Jonathan M

Reputation: 17451

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

Upvotes: 8

Related Questions