{"id":7202,"date":"2020-11-24T16:03:06","date_gmt":"2020-11-24T21:03:06","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=7202"},"modified":"2020-11-24T16:03:06","modified_gmt":"2020-11-24T21:03:06","slug":"vscode-search-replace-using-regex-capture-groups","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=7202","title":{"rendered":"VSCode Search\/Replace Using Regex Capture Groups"},"content":{"rendered":"<p>Regex adds a lot of flexibility to search\/replace operations. Capture groups allow you to manipulate the <em>found<\/em> data in the replacement text. As an example, I have simple mathematical equations that are not spaced out reasonably. I <em>could<\/em> replace &#8220;+&#8221; with &#8221; + &#8220;, &#8220;-&#8221; with &#8221; &#8211; &#8220;, &#8220;*&#8221; with &#8221; * &#8220;, &#8220;\/&#8221; with &#8221; \/ &#8220;, and &#8220;=&#8221; with &#8221; = &#8220;, but using a capture group to identify non-whitespace characters and the range of operators allows a single search\/replace operation to add spaces to my equations.<\/p>\n<p>Selecting the regex option (in blue below), I can use the regular expression (\\S+)([\\+,\\-,\\*,\\\/])(\\S+)=(\\S+) as my search string. This means the first capture group is one or more non-whitespace characters, the second capture group is one of the characters +,-,*,\/, the third capture group is one or more non-whitespace characters, there&#8217;s an equal sign (which I <em>could<\/em> make into a fourth capture group), and the fourth capture group is one or more non-whitespace characters.<\/p>\n<p>An alternate regex finds <em>zero<\/em> or more whitespace characters &#8212; (\\S*)([\\+,\\-,\\*,\\\/])(\\S*)=(\\S*)<\/p>\n<p><a href=\"https:\/\/www.rushworth.us\/lisa\/?attachment_id=7203\" rel=\"attachment wp-att-7203\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-7203\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2020\/11\/regexGroupSeachReplace-Search-1024x73.png\" alt=\"\" width=\"960\" height=\"68\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2020\/11\/regexGroupSeachReplace-Search-1024x73.png 1024w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2020\/11\/regexGroupSeachReplace-Search-300x21.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2020\/11\/regexGroupSeachReplace-Search-768x55.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2020\/11\/regexGroupSeachReplace-Search-750x54.png 750w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2020\/11\/regexGroupSeachReplace-Search.png 1312w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>The replacement text then uses each capture group &#8212;<strong> $1 $2 $3 = $4<\/strong> &#8212; to add spaces around the operators and around the equal sign.<\/p>\n<p><a href=\"https:\/\/www.rushworth.us\/lisa\/?attachment_id=7204\" rel=\"attachment wp-att-7204\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7204\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2020\/11\/regexGroupSeachReplace-Results.png\" alt=\"\" width=\"259\" height=\"118\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Regex adds a lot of flexibility to search\/replace operations. Capture groups allow you to manipulate the found data in the replacement text. As an example, I have simple mathematical equations that are not spaced out reasonably. I could replace &#8220;+&#8221; with &#8221; + &#8220;, &#8220;-&#8221; with &#8221; &#8211; &#8220;, &#8220;*&#8221; with &#8221; * &#8220;, &#8220;\/&#8221; &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[883,778,817],"class_list":["post-7202","post","type-post","status-publish","format-standard","hentry","category-coding","tag-regex","tag-vs-code","tag-vscode"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/7202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7202"}],"version-history":[{"count":2,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/7202\/revisions"}],"predecessor-version":[{"id":7206,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/7202\/revisions\/7206"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}