Replacing% uXXXX with the appropriate Unicode code in Ruby

I have file names that contain the substrings% uXXXX, where XXXX are hexadecimal numbers / digits, e.g.% u0151, etc. I got these file names using URI.unescape, which was able to replace the substrings% XX with the corresponding characters, but the% uXXXX substrings remained untouched. I would like to replace them with the corresponding Unicode codes using String # gsub. I tried the following but did not succeed:

"rep%u00fcl%u0151".gsub(/%u([0-9a-fA-F]{4,4})/,'\u\1')

I get this:

"rep\\u00fcl\\u0151"

Instead of this:

"repülő"
+4
source share
2 answers

Try this code:

string.gsub(/%u([0-9A-F]{4})/i){[$1.hex].pack("U")}

In the comments, cremno has a faster solution:

string.gsub(/%u([0-9A-F]{4})/i){$1.hex.chr(Encoding::UTF_8)}

bobince , .

+2

@cremno, :

gsub(/%u([0-9A-F]{4})/i) { $1.hex.chr(Encoding::UTF_8) }

:

s = "rep%u00fcl%u0151"
s.gsub(/%u([0-9A-F]{4})/i) { $1.hex.chr(Encoding::UTF_8) }
# => "repülő"
+1

All Articles