From: http://stackoverflow.com/questions/11257509/urldecoder-illegal-hex-characters-in-escape-pattern-for-input-string-p
Whoever created the URL should has percent encoded the by %
writing %25
.
Example Invalid URL
Http://example.com/test?q=%.P
Example valid URL
Http://example.com/test?q=%25.P
The answer provided by Mark Byers would work just fine if there ' re only chars this need to being escaped but would %
fail I F URL contains percent-encoded chars. To avoid the there ' s a little bit more work needed.
In Percent-encoding (url-encoding) only reserved and unreserved chars won ' t is percent-encoded.
ReservedChars:╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗║ ! ║ # ║$║ & ║ ‘ ║ ( ║ ) ║ * ║ + ║ , ║ / ║ : ║ ; ║ = ║ ? ║ @ ║ [ ║ ] ║╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝UnreservedChars:╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗║A║B║C║D║E║F║G║H║I║J║K║L║M║N║O║P║Q║R║╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗║S║T║U║V║W║X║Y║Z║A║B║C║D║E║F║G║H║I║J║╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗║K║L║M║N║O║P║Q║R║S║T║U║V║W║X║Y║Z║╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗║ 0 ║ 1 ║ 2 ║ 3 ║ 4 ║ 5 ║ 6 ║ 7║ 8 ║ 9 ║ - ║ _ ║ ║ ~ ║╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
According to RFC 3986 percent-encoded character have following format: % + hex
. So if you want to properly escape URLs that have unescaped %
chars without breaking the whole URL before actually Decodi Ng it, you need to replace only those signs that is not %
followed by Hex.
Finding substring that violates some the pattern is pretty easy task with regex. In this case pattern would look like this:
%(?![0-9a-fA-F]{2})
Sample:
Class Main{ Public Static voidMain(String[]Args) ThrowsJava.Lang.Exception { string URL = ; URL = Urlreplaceall ( "% (?! [0-9a-fa-f] {2}) " , ); system.. Printlnurl}} /span>
Urldecoder:illegal hex characters in Escape (%) pattern-for input string: