Blog

LET’S TALK
Regex usage in Integrated Development Environments

Regex usage in Integrated Development Environments

23/02/2021

The ancient scripts of Regular Expressions are always somewhat confusing for us, developers. Most of us use it only for input validation, but Regex has many interesting usages for various programming languages. Unfortunately, with so many other technologies, we have to keep up with, there’s often just too little time to take full advantage of it. In this article, I would like to show how we can use Regular Expressions to streamline our work in IDEs.

Note: these usages can be applied to any IDE that supports Regex searching. In this article, I will be using Visual Studio Code.

Checking text for potential PascalCase or camelCase errors

Each project has its own convention of file naming. It’s important not to overlook any misspelling because it can lead to errors in communication between the frontend and backend server. Fortunately, we can use our knowledge of Regular Expression to easily search through the whole file to look for potential spelling errors.

regex

 

In this case, we are searching for Uppercase letters succeeding the opening quotation mark (“), so we can look for PascalCase words that should start with lowercase instead (e. g. for camelCase).
\s*”[A-Z]
We can customize it by looking for camelCase words instead.

\s*”[a-z]

Using \s* lets us omit any whitespaces at the beginning of the line.

Generating enums on frontend from backend

On the backend, enums are often kept as numeric enums, while on the front we may need them in other forms. In this example, we can use Regex to easily convert numeric enums to string enums.

regex

Find: (\S+) = \d+

Replace: $1 = ‘$1’

If we want to do different conversions, there’s probably a Regular Expression for that. Let’s remove initializers all together from a given numeric enum.

regex
Find: (\S+) = \d+

Replace: $1

Finding commented code in our files

Before publishing a Pull Request with our work, we want to make sure there are no leftovers. When there are many file changes, it’s easy to overlook some comments, we have left during the development process. This, rather lengthy Regex (found here) will help us find different types of comments.

regex

Find: (/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)

Finding duplicate words inside a string in JSON file

Text rendered in our application comes from various resources. Before it is shaped into its final form, such text is often modified, copied, pasted, etc. However, such operations can lead to errors. With the help of this regex, we can look for duplicated words inside our file and delete the obsolete copy.

regex

Find: (\S+) = \d+

Replace: $1                                                         

Having to deal with all those complex problems that modern development throws at us is often an exhausting task. We should keep that in mind while using Regular Expressions. It’s obvious that we won’t remember all its basic rules, let alone some more complicated ones. One of the many reasons is that Regular Expressions are often one of many tools we are using during our work. So instead of fighting the impossible and remembering all those things, let’s just store the more useful Regex patterns and use them in our daily work. It will not only streamline our work with IDEs but also keep our sanity levels in place 🙂

how to regex

 

Let’s talk

Your message has been sent.
Thank you!