Regex 用elisp替换转义的unicode

Regex 用elisp替换转义的unicode,regex,emacs,unicode,elisp,Regex,Emacs,Unicode,Elisp,通过在emacs中调用google dictionary api, 我可以得到如下的回应 "entries": [{ "type": "example", "terms": [{ "type": "text", "text": "his grandfather\x27s \x3cem\x3ewords\x3c/em\x3e had been meant kindly", "language": "en" }] }] 正如您

通过在emacs中调用google dictionary api, 我可以得到如下的回应

"entries": [{
    "type": "example",
    "terms": [{
        "type": "text",
        "text": "his grandfather\x27s \x3cem\x3ewords\x3c/em\x3e had been meant kindly",
        "language": "en"
    }]
}]
正如您所看到的,“文本”中有转义的unicode。我想在函数中转换它们,如下所示

(defun unescape-string (string)
    "Return unescape unicode string"
    ...
)
(unescape-string "his grandfather\x27s \x3cem\x3ewords\x3c/em\x3e")
=> "his grandfathers's <em>words</em>"

(insert #x27)'
(insert #x27)'
(insert #x3c)<
(insert #x3e)>
(定义unescape字符串(字符串)
“返回unescape unicode字符串”
...
)
(unescape字符串“他的祖父\x27s\x3cem\x3ewords\x3c/em\x3e”)
=>“他祖父的话”
(插入“x27”)
(插入“x27”)
(插入#x3c)<
(插入#x3e)>
这是我试过的

  • 替换字符串中的regexp
  • 定制替换类
但是,我想我不知道如何将“\x123”替换为相应的unicode到缓冲区或字符串中


提前感谢似乎是最简单的方法:

(read (princ "\"his grandfather\\x27s \\x3cem\\x3ewords\\x3c/em\\x3e had been meant kindly\""))
;; "his grandfather's ώm>words</em> had been meant kindly"
(defun replace-c-escape-codes (input)
  (replace-regexp-in-string 
   "\\\\x[[:xdigit:]][[:xdigit:]]"
   (lambda (match)
     (make-string 1 (string-to-number (substring match 2) 16)))
   input))

(replace-c-escape-codes "his grandfather\\x27s \\x3cem\\x3ewords\\x3c/em\\x3e")
"his grandfather's <em>words</em>"