The power of C# regex


Let’s say you have a big chunk of text and you want to parse it to get some info. Or perhaps you even want to replace some pattern with another. Depending on how complex your extraction process is, the whole “split, search and recombine” workflow can be tedious and unoptimised. At times, it’s also pretty hard to know where to cut the content to parse the data you want.

Instead, you can use regular expressions, aka “regex”.

So, what’s a regex?

As explained in the wiki:

A regular expression (shortened as regex or regexp; also referred to as rational expression) is a sequence of characters that specifies a search pattern.

This tool comes from theoretical computer science and more specifically the subdomain of formal languages. Most programming languages have regex; and, of course, there are some standards that have emerged along the years.

You might have already come across regex: they are often written between slashes / (or as raw strings in Python, with the r'' syntax) and can look a bit weird, like this one:

visit