CategoriesAndroid StudioSoftware DevelopmentTips and Tricks

Regex as a Steroid for Find and Replace in IDE

This is a long story about a small trick I learned about using Regex with Find and Replace in the IDE.

Understanding Our Small Problem

I was writing a small python script to push make some requests to an endpoint for seeding my database.

I had my data in a json file so it was as simple as reading the json array and making a POST request with each JSON object.

Now while writing my function to convert the json event into the request Payload, I just copy pasted the Json object and made necessary changes, however python wasn’t happy about the “” to be used as the keys for my dictionary.

So what I essentially had to do was to convert all the keys from “key_name” to ‘key_name’ notice the change from ” to ‘.

Since this payload didn’t really have a lot of keys it would have taken me less than 2 minutes to do it by hand. But I realised, this is not the first time I am doing this and probably also not the last time.

So I decided to find a more efficient way of doing this.

Evolving our Regex Based Solution

The first thing that came to my mind was Find and Replace.

What I could have done is replaced all the double qoutes (“) with single qoutes (‘) but this is bad as I might screw up the script by replacing double qoutes where they might actually be needed.

I believe a lot of developers overlook the support of Regex in modern IDEs and Text Editors.

Leveraging the power of Regex matching with Find and Replace could help us solve this problem very easily. Most of the time I use Regex101 to build my regex patterns.

But this time, I just turned to the IDE and wrote the simple pattern that I was looking to match, which is \”([a-zA-Z]+)\”:

Don’t worry if you don’t have a lot of experience with Regex, I’ll break it down for you.

Decoding the Regex

Regex Pattern
  • \” -> Will try to match a double quote character.
  • ( -> marks the beginning of a capturing group
  • [ ] -> specifies a range of characters you want to match
  • a-z -> is the range of characters between a (ascii index 97) and z (ascii index 122)
  • A-Z -> is the range of characters between a (ascii index 65) and z (ascii index 90)
  • + -> is a quantifier that will try to match the previous character or range at least ones and then as many times until there is no longer a match.
  • ) -> marks the end of a capturing group
  • \” -> Will try to match a double quote character.
  • : -> Will try to match a colon character.

I can imagine some of you might be wondering what is the use of a capturing group here?

The capturing group actually will help us preserve the key name.

For example if the string was “username”, my goal is to modify it to ‘username’. So in order to preserve the key itself, I will capture it into a group and later use this while replacing the strings.

Another thing that might have caught your attention is the : (colon) why do I need to match this?

Well my python file has strings like

response = requests.request("POST", url, data=json.dumps(payload), headers=headers)

or

url = "http://" + BASE_URL + "/tags/"

and I don’t want to replace them with single quotes.

So I found that adding a colon at the end will make sure I am only finding the keys I am actually interested in modifying.

Now if you actually run the find command it will start showing you the results

Now to replace the matched strings, lets open the replace tool by hitting Cmd + R

And add this regex there

\’$1\’:

What this will do is the following:

  • it will add a single quote
  • then add the capturing group number 1
  • then add a single quote
  • then add a colon

You remember we added a Capturing group using these parenthesis ( ) ?

That is what helped us preserve the key name. Capturing groups are 1-indexed so this might be something you’d want to make a note of.

And you can refer to Capturing groups by a $ sign followed by the capturing group number.

Since we only had one capturing group we wrote $1.

And now you can see that IntelliJ is even showing us a preview of replace action will be.

Now I can finally hit Replace All and get the job done.

In The End, It Does Matter…

In the end I’d like to conclude with the fact that it is, these extra steps that we take in practice to solve problems that help us learn new things. Learnings compound over time. It is easier to learn things by solving problems rather than just by reading stuff.