StatusCake

Visual Studio 2019+ Regex Features

statuscake

Whether you’ve never heard of regex before, or use it in your code all the time, one thing is almost certain…it is not that easy to read! Case in point:

/^#?([a-f0-9]{6}|[a-f0-9]{3})$/

Thankfully the Visual Studio team at Microsoft have included several handy features inside the code editor to help using regex a little less painful. But first let’s quickly cover,

What is Regex?

Just in case the term is new to you, regex is a portmanteau of regular + expression. Regex is a set of characters that defines a specific search pattern in text, programmers will traditionally use regex to look for specific data patterns in strings.

For example, say I had some code that was reading a stream of data from a .txt file and the only data I want to save from this text file are GUID’s. To accomplish this, we can write a very simple and understandable piece of regex to match the standardised format of a GUID (xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx):

.{8}-.{4}-.{4}-.{4}-.{12}

This regex is made up of three components:

  • The period token  .  matches any single character 
  • The number in curly braces  {8}  represents how many reoccurring  .  ‘s to capture 
  • The hyphen   is not a regex token it will  just match a hyphen character exactly

In the screenshot below you can see it in action:

Regex patterns can range from very simple to very complex, you can learn more about Regex and test your own patterns here

Ok, on to the features!

Visual Studio Regex Features

Visual Studio Settings

First things first, you’ll want to make sure the following settings are enabled in your editor (they should be by default).

Regex colour formatting

Visual studio makes regex string patterns easier to distinguish by colourising all the groups, quantifiers, comments, etc.

Regex Options

You can pass in an optional RegexOptions parameter when initialising a new Regex().

Note: The regex colouriser won’t work until any errors on the line are resolved. Just in case you were wondering about the screenshot above.

A few examples of regex options would be:

RegexOptions.IgnoreCase : Use case-insensitive matching. By default, the casing conventions of the current culture are used.

RegexOptions .IgnorePatternWhitespace : Exclude unescaped white space from the pattern, and enable comments after a number sign (#).

RegexOptions .ECMAScript : Instructs the regex engine to behave like the ECMASCript/JavaScript regex engine.

Built-in Regex autocompletion

IntelliSense can assist you when writing your regex patterns. The autocomplete dropdown works inside the pattern string and is helpful to discover the syntax or description of how the tokens operate.

Catching Pattern Errors

To avoid you getting runtime errors, Visual Studio reports errors contained in the pattern in real-time as you code, and gives you advise on how you can fix it:

Highlighted backreferences

Sometimes it can be difficult to see the referenced group for a backreference in your pattern. Visual Studio helps you out here by highlighting both parts of the backreference for you, making it super clear.

Conclusion

I hope you’ve found this slightly shorter post useful. Regex can be an incredibly powerful tool when used correctly, and I’d like to cover it in more detail at a later date, so keep your eyes peeled! 

Share this

More from StatusCake

Engineering

Beyond Uptime: Building a Self-Healing OpenClaw Observability Stack

3 min read The allure of OpenClaw is undeniable. You deploy a highly autonomous, self-hosted AI agent, give it access to your repositories and inboxes, and watch it reason through complex workflows while you sleep. It is the dream of the ultimate 10x developer tool realized. But as any veteran DevOps engineer will tell you: running an LLM-backed

When AWS us-east-1 Fails, Much of the Internet Fails With It

7 min read There are cloud outages, and then there are us-east-1 outages. That distinction matters because failures in AWS’s Northern Virginia region rarely feel like ordinary regional incidents. They tend instead to expose something larger and more uncomfortable: too much of the modern internet still behaves as though one place is an acceptable concentration point for infrastructure,

In the Age of AI, Operational Memory Matters Most During Incidents

7 min read Artificial intelligence is making software easier to produce. That much is already obvious. Code that once took hours to scaffold can now be drafted in minutes. Boilerplate, integration logic, tests, refactors and small internal tools can be generated with startling speed. In some cases, even substantial pieces of implementation can be assembled quickly enough to

AI Didn’t Kill the SDLC. It Made It Harder to See

10 min read Whilst AI has compressed the visible stages of software delivery; requirements, validation, review and release discipline have not disappeared. They have been pushed into automation, runtime and governance. The real risk is not that the lifecycle is dead, but that organisations start acting as if accountability died with it. There is a now-familiar story about

When Code Becomes Cheap: The New Reliability Constraint in Software Engineering

4 min read How AI Is Shifting Software Engineering’s Primary Constraint For most of the history of software engineering, the primary constraint was production. Code was expensive, skilled engineers were scarce, and shipping features required concentrated human effort. Velocity was limited by how fast people could reason, implement, test, and deploy. That constraint shaped everything from team size,

Buy vs Build in the Age of AI (Part 3)

5 min read Autonomous Code, Trust Boundaries, and Why Governance Now Matters More Than Ever In Part 1, we looked at how AI has reduced the cost of building monitoring tools. Then in Part 2, we explored the operational and economic burden of owning them. Now we need to talk about something deeper. Because the real shift isn’t

Want to know how much website downtime costs, and the impact it can have on your business?

Find out everything you need to know in our new uptime monitoring whitepaper 2021

*By providing your email address, you agree to our privacy policy and to receive marketing communications from StatusCake.