The power of C# regex

<p>Let&rsquo;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 &ldquo;split, search and recombine&rdquo; workflow can be tedious and unoptimised. At times, it&rsquo;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,&nbsp;<em>aka</em>&nbsp;&ldquo;regex&rdquo;.</p> <h1>So, what&rsquo;s a regex?</h1> <p>As explained&nbsp;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&nbsp;standards&nbsp;that have emerged along the years.</p> <p>You might have already come across regex: they are often written between slashes&nbsp;<code>/</code>&nbsp;(or as raw strings in Python, with the&nbsp;<code>r&#39;&#39;</code>&nbsp;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 &mdash; RGB form.</p> <p>But how? What do all these weird parenthesis and curly braces mean? Why the&nbsp;<code>g</code>&nbsp;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>