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
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,
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:
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 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.
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
3 min read For any web developer, DevTools provides an irreplaceable aid to debugging code in all common browsers. Both Safari and Firefox offer great solutions in terms of developer tools, however in this post I will be talking about the highlights of the most recent features in my personal favourite browser for coding, Chrome DevTools. For something
6 min read There has certainly been a trend recently of using animations to elevate user interfaces and improve user experiences, and the more subtle versions of these are known as micro animations. Micro animations are an understated way of adding a little bit of fun to everyday user interactions such as hovering over a link, or clicking
2 min read Read about the latest websites that have experienced downtime including Netflix, Twitter, Facebook and more inside!
2 min read Read about how Google suffered an outage due to the soaring temperatures in the UK in July and how they rectified it right here!
3 min read See the results of our website downtime survey to see some of the most shocking and surprising stats! You won’t be disappointed.
6 min read Find out everything you need to know about Dark Mode and what you can do, as a developer, to make it easier to use.
Find out everything you need to know in our new uptime monitoring whitepaper 2021