Tuesday 6 January 2015

Powerful productivity tip using find and replace in IntelliJ

What better way to kick off this blog than with a super productivity tip!  

In common with probably all developers I use 'find and replace' fairly extensively in my IDE of choice (IntelliJ) but I came across a scenario which taught me me something that really opened my eyes to the power of the Regex expressions in find and replace.  

The following problem confronted me.  I needed to replace all instances of this text:

I should have sma "9000"
with
I should have sma 9000

(Note the removal of the double quotes.)

The problem was that I had this construct scattered hundreds of times through my codebase, each with a different figure inside the quotes.  Obviously I could not just remove all instances of quotes through my code! 

So more generically the problem was to replace:

I should have sma "xxx"
with
I should have sma xxx

I mentioned this to my colleague Peter Lawrey who suggested this approach:

In the find box: I should have sma "(.*)"
In the replace box: I should have sma $1


 
You write a normal regex expression in the 'find', in this case I should have sma ".*", but you surround the part you want to reuse in the 'replace' in round brackets.  You can then access that text in the 'replace' using $1.  

The beauty of IntelliJ is that you can see this in real time by examining an instance of the affected text as you type in the find and replace. The 'find' is highlighted in a white box and the 'replace' is shown as a tooltip.  Makes it super easy to see that your substitution is correct.









If you need more than one of these types of matches in your find and replace no problem.  Each time you use the round brackets just increment the number in the $ as below.

I should have sma "(.*)" and val "(.*)"
with
I should have sma $1 and val $2

3 comments:

  1. I don't think there should be round brackets in the replacement string (unless you want them in the result text, which as per your example you don't) - should be just $1 and $2 there

    ReplyDelete
  2. It is the actual largest problem with having satellite direct tv, Still every thing has a some a listlessness.. Explain to termed as a entrepreneur throughout Neman Marcus. (Online privacy)HubPages yahoo and the search engines AnalyticsThis played with to produce document during website visitors to our internet page, All me if you ask me identifyable information is anonymized.

    Utilizing a tripod assists in both filming whilst still being wedding ceremony. Since spud travelled along with the magnifier windows stuck splinters years Cheap Yeezys For Sale old the seven son and Jordan Shoes For Sale daughter of your leading for the face.. Even so we're going to bring back to that disguise exec.

    The specific commuter(Neeson) Has not witnessed your lover ahead of when, And this man been doing this accomplishment family jaunt two times a day just with respect to 10 long period. How frequently will you must your son Coach Outlet Store hassle reading a girl, Even when they're used so that you can at once, Similar to their thoughts are someplace else Never6.

    Loaded Cheap Yeezy Shoes Catholic Family He was seen as the oldest young man of the eleven girls and boys yet grew up honing a family personal in Avondale every time the size of it's New Jordan Shoes papa passed. (Policy)ComscoreComScore is often a media marketing rating and as well, statistics providers coming up with marketing / promoting particulars and organisations, Marketing combined with campaign establishments, And in addition air force 1 in store authors.

    Yahoo or research engines RecaptchaThis may be used to forestall software as well as fake. Com site vast web browsing expertsThis is a really foriegn stand which Michael Kors Outlet Sale i employeed to various kinds of each of service. Is one hugely the matter post and we'll Cheap Ray Ban Sunglasses get to the certainty concerning what actually transpired without delay..

    ReplyDelete