{"id":2200,"date":"2020-11-08T10:25:58","date_gmt":"2020-11-08T09:25:58","guid":{"rendered":"https:\/\/digitalia.culturanuova.net\/blog\/?p=2200"},"modified":"2021-03-09T07:59:13","modified_gmt":"2021-03-09T06:59:13","slug":"regex-keep-a-part-of-a-string","status":"publish","type":"post","link":"https:\/\/digitalia.culturanuova.net\/blog\/?p=2200","title":{"rendered":"regex replace keeping a part of a string"},"content":{"rendered":"\n<p>Today I managed to replace (in html file links and anchors) absolute paths with relative ones.<\/p>\n\n\n\n<p>I needed to <strong>keep<\/strong> the anchors and links number.<\/p>\n\n\n\n<p>I did so:<\/p>\n\n\n\n<p><b>search<\/b>: <code>href=\"#_ftn([0-9])\"&gt;<\/code><br>\n<b>replace<\/b>: <code> href=\"#_ftnref\\1\" id=\"_ftn\\1\"&gt;<\/code><\/p>\n\n\n\n<p>In this way href=&#8221;#_ftn1 was replaced by &nbsp;<code>href=\"#_ftnref1\" id=\"_ftn1\"<\/code>,  href=&#8221;#_ftn2 was replaced by &nbsp;<code>href=\"#_ftnref2\" id=\"_ftn2\"<\/code>, and so on.<\/p>\n\n\n\n<p>For numbers with two digits<\/p>\n\n\n\n<p><strong>search<\/strong>:&nbsp;<code>href=\"#_ftn([0-9][0-9])\"&gt;<\/code><br><strong>replace<\/strong>:&nbsp;<code>href=\"#_ftnref\\1\" id=\"_ftn\\1\"&gt;<\/code><\/p>\n\n\n\n<p>Only the <strong>search<\/strong> string is to be modified, if you have more digits (f.e. <code>[0-9][0-9][0-9][0-9]<\/code> if you have numbers with four digits, such as 1276), <strong>not the replace one<\/strong> (so don&#8217;t write <code>\\1\\1<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">digits within brackets<\/h2>\n\n\n\n<p>To replace digits within brackets you have to escape the brackets with &#8216;\\&#8217;, thus: &#8216;\\(&#8216;, or &#8216;\\[&#8216;. For example, if you plan to convert every &#8216;(some digits) &#8216; with &#8216;&lt;\/p&gt;&lt;p&gt;(some digits)&#8217;, you should use this code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>find: \\((&#91;0-9])\\)\nreplace: &lt;\/p&gt;&lt;p&gt;(\\1)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Today I managed to replace (in html file links and anchors) absolute paths with relative ones. I needed to keep the anchors and links number. I did so: search: href=&#8221;#_ftn([0-9])&#8221;&gt; replace: href=&#8221;#_ftnref\\1&#8243; id=&#8221;_ftn\\1&#8243;&gt; In this way href=&#8221;#_ftn1 was replaced by &nbsp;href=&#8221;#_ftnref1&#8243; id=&#8221;_ftn1&#8243;, href=&#8221;#_ftn2 was replaced by &nbsp;href=&#8221;#_ftnref2&#8243; id=&#8221;_ftn2&#8243;, and so on. For numbers with two &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/digitalia.culturanuova.net\/blog\/?p=2200\" class=\"more-link\">Leggi tutto<span class=\"screen-reader-text\"> &#8220;regex replace keeping a part of a string&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[565,566],"tags":[227],"class_list":["post-2200","post","type-post","status-publish","format-standard","hentry","category-office","category-webmaster","tag-regex"],"_links":{"self":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2200"}],"version-history":[{"count":9,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2200\/revisions"}],"predecessor-version":[{"id":2396,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2200\/revisions\/2396"}],"wp:attachment":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}