Leverage Regular Expression in Visual Studio Code

Regular Expression (RegEx) is one of the feature that available in Visual Studio (VS) Code. This feature helps me to narrow down the scope when performing searching in code-base. To be honest, I was always trying to avoid using RegEx cause it seems so complicated and hard to remember the syntax. But I finally embrace that and it’s actually not scary at all. You doesn’t need to be expert in RegEx to start using this feature.

Regular Expression

regular expressionregex or regexp (sometimes called a rational expression) is a sequence of characters that define a search pattern. Usually this pattern is used by string searching algorithms for “find” or “find and replace” operations on strings, or for input validation. It is a technique developed in theoretical computer science and formal language theory.


RegEx allows us search specific strings by defining each single characters. For instance, I wanted to search specific URLs. Below is the sample of text we want to search from.

Sample text we will be using to search using RegEx

ReGex query #1: www\.google\.com
To represent period in RegEx, we need to escape the dot by using a slash (\). The above regex (\.) metacharacter is representing period (.) character.

Result #1
– www.google.com

Screenshot of regex search with results in VS Code. Note: to enable regular expression searching, click on [.*].

RegEx query #2: w{3}\.google\.com
The result for the second RegEx will be similar as the first one. I replaced www with w{3}. This ({3}) is the repetition that we can assign to any character.

Result #2
– www.google.com (similar as above)

Screenshot of regex search with results in VS Code

RegEx query #3: https://w{3}\.\w+\.com
In the third regex, I replaced google keyword with \w+ which actually consist of two metacharacter, (1) \w and (2) + . \w represent any alphanumeric character while + is the quantifier to indicate the metacharacter is having one or more repetitions. Other option to this metacharacter is * to indicate the metacharacter is having zero or more repetitions. We can’t use * since we need at least one alphanumeric character in between www.{here}.com.

Result #3
– https://www.facebook.com
– https://www.digitalocean.com

Screenshot of regex search with results in VS Code

If you wanted to learn more, you can head over to this tutorial and start learning regex. The tutorial provide a good set of quizzes for each lessons.

The Problems

So I had this assignment which I need to clean-up the legacy code (let say Application X). This initiative came up when I mentioned to my superior that in order to use Git as the code version control, we need to make sure the Application X is able to run in local machine (which is developer’s laptop).

There are many factors that cause the Application X not capable of doing that such as old version of PHP, old version of CodeIgniter framework version, and many hard-coded stuffs (especially the environment and integrations with other systems).

  • Replace all hard-coded URL with environment configuration variable.
  • Implementation and usage of the URL in various places is differ.
  • Inconsistency of environment configuration variables. Using the same URL in form of IP address and domain name. Some places use while other places use https://test.com which is pointing to the same location.

Strategy & Actions

Clean Up

The legacy code has huge files and it makes even bigger when all the backup files with dates or developer’s name also inside the folder. Since the Application X is not using any version control tools, I guess this should be expected. Therefore, I decided to clean up the mess by removing unnecessary files first. Otherwise, it will be very hard for me to handle keyword replacement in multiple redundant files.

Searching with RegEx

Once the code is cleaned, I utilize RegEx in VS Code to start find and replace the URLs, IPs and those environment configs related stuffs with variables in all files in the code-base. As a result, now the legacy code can run in multiple environments.


RegEx is powerful. There are more things that we can do with RegEx and I think I just learn some of it, more to be explored. I’m glad that I decided to invest a couple of hours to learn ReGex (at least get the gist) and utilize this feature in VS Code. This feature totally helps me to get my works done. Note to myself, never afraid to learn something new. It sometimes will helps us and if not, we just expand our knowledge and why not share that knowledge with others.