The power of C# regex
<p>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.</p>
<p>Instead, you can use regular expressions, <em>aka</em> “regex”.</p>
<h1>So, what’s a regex?</h1>
<p>As explained in the wiki:</p>
<blockquote>
<p><em>A regular expression (shortened as regex or regexp; also referred to as rational expression) is a sequence of characters that specifies a search pattern.</em></p>
</blockquote>
<p>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.</p>
<p>You might have already come across regex: they are often written between slashes <code>/</code> (or as raw strings in Python, with the <code>r''</code> syntax) and can look a bit weird, like this one:</p>
<pre>
/(#?\w{6})|(rgb\s?\(\s?(\d{1,3},\s?){2}\d{1,3}\s?\))/g</pre>
<p>As we will see in this article, all the bizarre characters in this string are actually tokens and specific patterns that the regex evaluation process will use to test whether or not your input string corresponds to this search pattern. For example, the above regex checks if your text contains one or more occurrences of a colour written in hexadecimal or 255 — RGB form.</p>
<p>But how? What do all these weird parenthesis and curly braces mean? Why the <code>g</code> in the end? Where is the colour in that bit of text? Well - time to dive into the logic of regex!</p>
<p><a href="https://medium.com/c-sharp-progarmming/the-power-of-c-regex-a585484dbc90">Read More</a></p>