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 – for example, change “@Api(value” to “@Tag(name”. Those were easy and I could just use replace in files.

But then I had to change
@ApiResponse(code = "401", message
with
@ApiResponse(responseCode = "401", description

But the response code could be anything, not just 401! So I couldn’t 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’t even tell you. Well, actually I can, really. It was a LOT. (Sit for a moment and relish the eloquence!)

This made me very, very happy. So I wanted to share!

Note that I am not going to be trying to teach regular expressions. There is already a lot online about them, and I’m just not going to go there.

In the IntelliJ menus, select Edit->Find->Find in Files to get a text box to search across all files. Click the little box below the file mask with “.*” in it. The text you search for will now be used as a regular expression.

RegEx Button
RegEx Button

So to show RegEx searches – say you want to search for strings like this:
@ApiResponse(code = "200", message
@ApiResponse(code = "401", message
@ApiResponse(code = "404", message
@ApiResponse(code = "500", message

(There is more to the string, thus the lack of a closing parenthesis in the search term.)

Let’s say I want to find the 4xx strings, but not any others. That means making a RegEx around the code – basically I want a 4, then two more numbers. RegEx for that would be:
4[0-9]{2}
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:
@ApiResponse(code = "4[0-9]{2}", message
(Yes, you could do shorter, but I’m trying to build up to something!)

RegEx searches like that work to search just in the current file (ie, when you search with ⌘F) as well as searching across files (⌃⇧F). 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.

So the search string I used to find all of these (I actually wanted them all, not just the 4xx ones!) was:
@ApiResponse(code = [0-9]{3}, message

Well that’s 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 “capturing group”. You can have multiple of them, but for this example we only need one:

@ApiResponse(code = ([0-9]{3}), message

To refer to that capturing group, you just need to use an argument like “$1”.

So to do the replacement above, use IntelliJ’s “Replace in Files” to search for:
@ApiResponse(code = ([0-9]{3}), message
with a replacement string of
@ApiResponse(responseCode = "$1", description

Example RegEx Replacement
Example RegEx Replacement

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!

This is one of those things you don’t need that often, but when you do – it’s huge.

If you want to read more:

https://www.jetbrains.com/help/idea/tutorial-finding-and-replacing-text-using-regular-expressions.html

Categorized in:

Tagged in: