{"id":11041,"date":"2022-12-27T12:55:21","date_gmt":"2022-12-27T17:55:21","guid":{"rendered":"https:\/\/irreal.org\/blog\/?p=11041"},"modified":"2022-12-27T12:55:21","modified_gmt":"2022-12-27T17:55:21","slug":"regex-search-and-replace-in-emacs","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=11041","title":{"rendered":"Regex Search and Replace in Emacs"},"content":{"rendered":"<p>Tony Aldon has a short video on <a href=\"https:\/\/www.youtube.com\/watch?v=Zm0XjD1sA_Q\">a couple of alternate ways to do regex search and replace in Emacs<\/a>. The straightforward way to do that is to use <code>query-replace-regexp<\/code> (<kbd>Ctrl<\/kbd>+<kbd>Meta<\/kbd>+<kbd>%<\/kbd>) but you can do the same operation in steps by first doing <code>isearch-forward-regexp<\/code> (<kbd>Ctrl<\/kbd>+<kbd>Meta<\/kbd>+<kbd>s<\/kbd>) to make sure you\u2019re finding the right search object and then calling <code>query-replace-regexp<\/code> to specify the replacement. That\u2019s not as wasteful as it seems because it will remember the search string from the <code>isearch-forward-regexp<\/code> and you need only specify the substitution.<\/p>\n<p>Aldon gives an example of this in operation so you can see exactly how it works. Take a look at the video to see the details.<\/p>\n<p>The second method is essentially the same except that it uses the package <a href=\"https:\/\/github.com\/benma\/visual-regexp.el\">visual-regexp<\/a> to get immediate feedback on what the regular expression will match as you type it. That can be pretty handy if Emacs regular expressions are your second choice for regex work and you don\u2019t use them all the time. Again, take a look at the video to see the package in action.<\/p>\n<p>Most of the time you\u2019re probably better off to just use <code>query-replace-regexp<\/code> but if you\u2019re unsure of the regex, the two step process can make sense. The first method uses functions that are all built-in so it\u2019s always available. If you want to see what\u2019s going to be matched as you enter the regex, the visual-regexp package may be for you.<\/p>\n<p>The video is only 8 minutes so it should be easy to fit in.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tony Aldon has a short video on a couple of alternate ways to do regex search and replace in Emacs. The straightforward way to do that is to use query-replace-regexp (Ctrl+Meta+%) but you can do the same operation in steps &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=11041\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[18],"class_list":["post-11041","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\/11041","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=11041"}],"version-history":[{"count":1,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/11041\/revisions"}],"predecessor-version":[{"id":11042,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/11041\/revisions\/11042"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}