Regex to find encoded string in a URL in java
I need to find whether the URL is encoded or not. As the input is dynamic it will be helpful if i know the regex to check it.
example -
www.test.com/?t=%E3%83%81%E3%82%B7%E3%83%BA%E3%83%B3%E3%83%9D%E3%83%BC%E3%83%ABThanks in ad开发者_如何学Govance
You could simply use URLDecoder to check whether the URL contained encoded parts or not rather than building a custom regular expression:
class UrlTest {
public static void main(String[] args) throws java.io.UnsupportedEncodingException {
String url = "http://example.com/%20foo";
if(url.equals(java.net.URLDecoder.decode(url, "UTF-8"))) {
System.out.println("URL didn't contain encoded parts.");
} else {
System.out.println("URL contained encoded parts.");
}
}
}
// regex for attempting to use any URL encoding, or .. for directory traversal
.*%[0-9a-fA-F]+|\.\.
If you are asking because you want to encode the url if it is not already encoded, then the easiest thing to do is decode it then encode it again. That way you are guaranteed to end up with an encoded url.
Just look for a '%' followed by [0-F] - you'll know.
精彩评论