IntelliJ IDEA Find & Replace using Regex

Recently I had to convert some documents which was on HTML format to Asciidoc format. An awesome tool called Pandoc saved me a lot of work. So after using that tool I was left to do a bit of final touches which included a lot of find and replace kind of work. I had actually set this up as a maven project with AsciiDoctor in IntelliJ IDEA so that is where I got to use this cool feature, find and replace with regex.

Here I’ll show a few examples as some of those are not that well documented.

Say  this here, “image:/download/attachments/983129/address-state-chart.png?version=3&modificationDate=1318249062000&api=v2[image,title=”sometitle”]”
is the string that I want to manipulate.

Ex01: Now if I want to get rid of this, “/download/attachments/some_digits” part in a set of similar strings.

/download/attachments/983129

So in find section (you get this by CTRL + F for a file or CTRL + SHIFT + F by selecting a directory) you can select “regex” option. Then you can type any regex which is related to your pattern. So here I used following pattern.

/download/attachments/\d*

Ex02: Here is the most coolest feature. Say you want to change all string which are in double quotations to single quotations. Basically it’s like changing,

"mytitle1"   to 'mytitle1'
"mytitleZ" to 'mytitleZ'

kind of thing. Now as your regex pattern in find you could use,

"(\S*)" 

and use

'$1'

as the string in replace. This will do the trick. Here’s how it works.
In strings under double quotations, the middle part will be the one we use as $1 in replace section. (Basically the part enclosed by brackets in regex is assigned to variable $1 like that.) So simply any string with double quotations will be replaced with single quotations using this.

I got to know it from this Stackoverflow answer. Check that for more info. Cheers!

Thank you JetBrains for letting me use IntelliJ IDEA for free as a student.

~Rajind Ruparathna

Leave a comment