{"id":267,"date":"2011-09-23T10:26:00","date_gmt":"2011-09-23T14:26:00","guid":{"rendered":"http:\/\/irreal.org\/blog\/?p=267"},"modified":"2011-12-26T18:00:40","modified_gmt":"2011-12-26T23:00:40","slug":"xahs-emacs-quiz","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=267","title":{"rendered":"Xah&#8217;s Emacs Quiz"},"content":{"rendered":"\n<p>Xah Lee has another Emacs <a href=\"http:\/\/xahlee.blogspot.com\/2011\/09\/emacs-quiz-replacing-html-entities-with.html\">challenge<\/a>. This time, it&#8217;s to replace entries of the form <\/p>\n<pre class=\"src src-html\">&lt;<span style=\"color: #0000ff;\">tr<\/span>&gt;&lt;<span style=\"color: #0000ff;\">td<\/span>&gt;pound&lt;\/<span style=\"color: #0000ff;\">td<\/span>&gt;&lt;<span style=\"color: #0000ff;\">td<\/span>&gt;&amp;#163;&lt;\/<span style=\"color: #0000ff;\">td<\/span>&gt;&lt;<span style=\"color: #0000ff;\">td<\/span>&gt;pound sign, U+00A3&lt;\/<span style=\"color: #0000ff;\">td<\/span>&gt;&lt;\/<span style=\"color: #0000ff;\">tr<\/span>&gt;\r\n<\/pre>\n<p> with entries like this: <\/p>\n<pre class=\"src src-html\">&lt;<span style=\"color: #0000ff;\">tr<\/span>&gt;&lt;<span style=\"color: #0000ff;\">td<\/span>&gt;pound&lt;\/<span style=\"color: #0000ff;\">td<\/span>&gt;&#163;&lt;<span style=\"color: #0000ff;\">td<\/span>&gt;pound sign, U+00A3&lt;\/<span style=\"color: #0000ff;\">td<\/span>&gt;&lt;\/<span style=\"color: #0000ff;\">tr<\/span>&gt;\r\n<\/pre>\n<p> This is, presumably, a small tweaking of Lee&#8217;s very useful page on <a href=\"http:\/\/xahlee.org\/comp\/unicode_html_entities.html\">HTML\/XML Entities<\/a> that I&#8217;ve written about <a href=\"http:\/\/irreal.org\/blog\/?p=37\">before<\/a>. <\/p>\n<p> It would be reasonably easy to do this by writing some Emacs Lisp but I wanted to see if I could do it interactively. It turns out to be pretty simple using <code>query-replace-regexp<\/code>. The secret is to use <code>\\,<\/code> <i>expr<\/i> as the replacement string in <code>query-replace-regexp<\/code>. With that replacement string, when the regular expression is matched <i>expr<\/i> is called to provide the replacement. I used <\/p>\n<pre class=\"src src-emacs-lisp\">&amp;#\\([0-9]+\\)<span style=\"color: #b22222;\">;<\/span>\r\n<\/pre>\n<p> as the regular expression and <\/p>\n<pre class=\"src src-emacs-lisp\">\\,(format <span style=\"color: #8b2252;\">\"%c\"<\/span> \\#1)\r\n<\/pre>\n<p> as the replace string. The <code>\\#1<\/code> takes the first subgroup and converts it to a number. Then the rest of the <code>format<\/code> statement delivers that unicode character as the replacement text. <\/p>\n<p> The careful reader will note that this doesn&#8217;t give quite what Lee asked for because the <code>&lt;td&gt;...&lt;\/td&gt;<\/code> tags are still there. I&#8217;m guessing that Lee really wanted them there but if not, it&#8217;s easy to get rid of them by using <\/p>\n<pre class=\"src src-emacs-lisp\">&lt;td&gt;&amp;#\\([0-9]+\\)<span style=\"color: #b22222;\">;<\/span><span style=\"color: #b22222;\">&lt;\/td&gt;<\/span>\r\n<\/pre>\n<p> as the regular expression. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Xah Lee has another Emacs challenge. This time, it&#8217;s to replace entries of the form &lt;tr&gt;&lt;td&gt;pound&lt;\/td&gt;&lt;td&gt;&amp;#163;&lt;\/td&gt;&lt;td&gt;pound sign, U+00A3&lt;\/td&gt;&lt;\/tr&gt; with entries like this: &lt;tr&gt;&lt;td&gt;pound&lt;\/td&gt;&#163;&lt;td&gt;pound sign, U+00A3&lt;\/td&gt;&lt;\/tr&gt; This is, presumably, a small tweaking of Lee&#8217;s very useful page on HTML\/XML Entities that &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=267\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[18],"class_list":["post-267","post","type-post","status-publish","format-standard","hentry","category-general","tag-emacs"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=267"}],"version-history":[{"count":6,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/267\/revisions"}],"predecessor-version":[{"id":472,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/267\/revisions\/472"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}