{"id":957,"date":"2024-01-30T18:58:00","date_gmt":"2024-01-30T18:58:00","guid":{"rendered":"https:\/\/www.evan.org\/ghg\/?p=957"},"modified":"2024-01-30T19:11:08","modified_gmt":"2024-01-30T19:11:08","slug":"intellij-regex-replacement","status":"publish","type":"post","link":"https:\/\/www.evan.org\/ghg\/java\/intellij-regex-replacement\/","title":{"rendered":"IntelliJ RegEx Replacement"},"content":{"rendered":"\n<p>So first, why this is great. I had to upgrade Swagger from 1.6.6 to 2.2.9, which is very much a breaking change which required changing a LOT of code. But the changes were very formulaic &#8211; for example, change &#8220;@Api(value&#8221; to &#8220;@Tag(name&#8221;. Those were easy and I could just use replace in files.<\/p>\n\n\n\n<p>But then I had to change<br><code>@ApiResponse(code = \"401\", message<\/code><br>with<br><code>@ApiResponse(responseCode = \"401\", description<\/code><\/p>\n\n\n\n<p>But the response code could be anything, not just 401! So I couldn\u2019t just replace the strings, as it would lose the response code value. But with regular expression search and replace, I could replace the string and keep the value! This saved me so much time I can&#8217;t even tell you. Well, actually I can, really. It was a LOT. (Sit for a moment and relish the eloquence!)<\/p>\n\n\n\n<p>This made me very, very happy. So I wanted to share!<\/p>\n\n\n\n<p>Note that I am not going to be trying to teach regular expressions. There is already a lot online about them, and I&#8217;m just not going to go there.<\/p>\n\n\n\n<p>In the IntelliJ menus, select <em>Edit-&gt;Find-&gt;Find in Files<\/em> to get a text box to search across all files. Click the little box below the file mask with &#8220;.*&#8221; in it. The text you search for will now be used as a regular expression.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"689\" height=\"326\" src=\"https:\/\/www.evan.org\/ghg\/wp-content\/themes\/maktub\/assets\/images\/transparent.gif\" data-lazy=\"true\" data-src=\"https:\/\/www.evan.org\/ghg\/wp-content\/uploads\/2024\/01\/FindInMultipleFiles.png\" alt=\"RegEx Button\" class=\"wp-image-955\" data-srcset=\"https:\/\/www.evan.org\/ghg\/wp-content\/uploads\/2024\/01\/FindInMultipleFiles.png 689w, https:\/\/www.evan.org\/ghg\/wp-content\/uploads\/2024\/01\/FindInMultipleFiles-300x142.png 300w\" data-sizes=\"(max-width: 689px) 100vw, 689px\" \/><figcaption class=\"wp-element-caption\">RegEx Button<\/figcaption><\/figure>\n\n\n\n<p>So to show RegEx searches &#8211; say you want to search for strings like this:<br><code>@ApiResponse(code = \"200\", message<br>@ApiResponse(code = \"401\", message<br>@ApiResponse(code = \"404\", message<br>@ApiResponse(code = \"500\", message<\/code><br>(There is more to the string, thus the lack of a closing parenthesis in the search term.)<\/p>\n\n\n\n<p>Let\u2019s say I want to find the <strong>4xx<\/strong> strings, but not any others. That means making a RegEx around the code &#8211; basically I want a 4, then two more numbers. RegEx for that would be:<br><code>4[0-9]{2}<\/code> <br>The [0-9] means any character in the character set from 0 to 9, and the {2} means two of them. So to make a full search string to find the 4xx values:<br><code>@ApiResponse(code = \"4[0-9]{2}\", message<\/code><br>(Yes, you could do shorter, but I\u2019m trying to build up to something!)<\/p>\n\n\n\n<p>RegEx searches like that work to search just in the current file (ie, when you search with \u2318F) as well as searching across files (\u2303\u21e7F). Searching inside a file while you construct the regex is a great way to check what you wrote as IntelliJ highlights what it finds as you type before you move to searching through all files.<\/p>\n\n\n\n<p>So the search string I used to find all of these (I actually wanted them all, not just the 4xx ones!) was:<br><code>@ApiResponse(code = [0-9]{3}, message<\/code><\/p>\n\n\n\n<p>Well that\u2019s great, I can find what I want to replace, but how do I retain the code value? This is where RegEx Capturing Groups come into play. When you put something into parenthesis, RegEx stores the matched value into a &#8220;capturing group&#8221;. You can have multiple of them, but for this example we only need one:<\/p>\n\n\n\n<p><code>@ApiResponse(code = ([0-9]{3}), message<\/code><\/p>\n\n\n\n<p>To refer to that capturing group, you just need to use an argument like &#8220;$1&#8221;.<\/p>\n\n\n\n<p>So to do the replacement above, use IntelliJ\u2019s \u201cReplace in Files\u201d to search for:<br><code>@ApiResponse(code = ([0-9]{3}), message<\/code><br>with a replacement string of<br><code>@ApiResponse(responseCode = \"$1\", description<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"689\" height=\"507\" src=\"https:\/\/www.evan.org\/ghg\/wp-content\/themes\/maktub\/assets\/images\/transparent.gif\" data-lazy=\"true\" data-src=\"https:\/\/www.evan.org\/ghg\/wp-content\/uploads\/2024\/01\/RegExReplaceExample.png\" alt=\"Example RegEx Replacement\" class=\"wp-image-956\" data-srcset=\"https:\/\/www.evan.org\/ghg\/wp-content\/uploads\/2024\/01\/RegExReplaceExample.png 689w, https:\/\/www.evan.org\/ghg\/wp-content\/uploads\/2024\/01\/RegExReplaceExample-300x221.png 300w\" data-sizes=\"(max-width: 689px) 100vw, 689px\" \/><figcaption class=\"wp-element-caption\">Example RegEx Replacement<\/figcaption><\/figure>\n\n\n\n<p>I mean check THAT out! Each of them will get replaced, and the code specified in the original string will get retained- and IntelliJ even shows you what it will do so that you can check before you fire the whole thing off!<\/p>\n\n\n\n<p>This is one of those things you don&#8217;t need that often, but when you do &#8211; it&#8217;s huge. <\/p>\n\n\n\n<p>If you want to read more:<\/p>\n\n\n\n<p><a href=\"https:\/\/www.jetbrains.com\/help\/idea\/tutorial-finding-and-replacing-text-using-regular-expressions.html\">https:\/\/www.jetbrains.com\/help\/idea\/tutorial-finding-and-replacing-text-using-regular-expressions.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>RegEx search and replace with capture groups is not only CRAZY awesome, but just saying those words will up your nerd cred.<\/p>\n","protected":false},"author":1,"featured_media":954,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[28,20],"tags":[29],"_links":{"self":[{"href":"https:\/\/www.evan.org\/ghg\/wp-json\/wp\/v2\/posts\/957"}],"collection":[{"href":"https:\/\/www.evan.org\/ghg\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.evan.org\/ghg\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.evan.org\/ghg\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.evan.org\/ghg\/wp-json\/wp\/v2\/comments?post=957"}],"version-history":[{"count":2,"href":"https:\/\/www.evan.org\/ghg\/wp-json\/wp\/v2\/posts\/957\/revisions"}],"predecessor-version":[{"id":959,"href":"https:\/\/www.evan.org\/ghg\/wp-json\/wp\/v2\/posts\/957\/revisions\/959"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.evan.org\/ghg\/wp-json\/wp\/v2\/media\/954"}],"wp:attachment":[{"href":"https:\/\/www.evan.org\/ghg\/wp-json\/wp\/v2\/media?parent=957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.evan.org\/ghg\/wp-json\/wp\/v2\/categories?post=957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.evan.org\/ghg\/wp-json\/wp\/v2\/tags?post=957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}