Mastering Regular Expressions: A Comprehensive Guide for Effective Pattern Matching, Extracting an

<p>Yeah, I know Regexes are scary monsters for the most of beginners in IT, at least for me they were. A good point is: when I&#39;m talking about IT, it&#39;s IT in general, not only about programming. Didn&#39;t you know you can use this outside the code? You can! A good example for that is Microsoft Excel, where functions like&nbsp;<em>RegExpMatch</em>,&nbsp;<em>RegExpExtract</em>&nbsp;and&nbsp;<em>RegExpReplace&nbsp;</em>may be used for a lot of use cases. So, this article doesn&#39;t need to be read only by programmers. Be aware, although this text is not driven to a specific language or software, to demonstrate the idea I&rsquo;m going to following the JavaScript way and DevTools runtime to represent and test this DSL for you.</p> <p>The first point is: what is a Regular Expression? Well, a regular expression is a&nbsp;<em>DSL</em>&nbsp;(Domain Specific Language) what means that it&#39;s a way to express something through its syntax and semantics. The theory it&#39;s wonderful, but let us see more about it.</p> <h2>Diving deep</h2> <p>Imagine that you need to match the following string list in a unique If-Else clause: &quot;I want a new book&quot;, &ldquo;I Want A new BOOK&rdquo; and &ldquo;i wANt a nEw boOk&rdquo;. Another point is: you are not allowed to use UpperCase and LowerCase functions. Of course you can find other ways to do that, like using ASCII to compare or calculate the equality, but why not to add a new item called regex in our toolbox as IT professionals? To resolve this problem I can simply use the regex:&nbsp;<em>/i want a new book/i.&nbsp;</em>Isn&rsquo;t that a common string? But what does that mean? No, that&#39;s not a common string, it&#39;s a regex. Firstly, this regex can be interpreted by the text used between the slashes &quot;i want a new book&quot; that&#39;s the &quot;core&quot; of the regex, that is, what we need to match. Secondly, there are the two slashes around the &quot;core&quot;, and here we have a important point, depending on the case and language this will or not be used, for example:</p> <p><strong>Languages that typically use slashes around regex:</strong></p> <ul> <li><strong>JavaScript Example</strong>:&nbsp;<code>/regex_pattern/</code></li> <li><strong>Python Example (with the&nbsp;</strong><code><strong>re</strong></code><strong>&nbsp;module):</strong>&nbsp;<code>re.search(r&#39;regex_pattern&#39;, input_string)</code></li> <li><strong>PHP Example:</strong>&nbsp;<code>preg_match(&#39;/regex_pattern/&#39;, $input_string)</code></li> <li><strong>Perl Example:&nbsp;</strong><code>$input_string =~ /regex_pattern/</code></li> <li><strong>Ruby Example:</strong>&nbsp;<code>/regex_pattern/</code></li> <li><strong>Java Example (with the&nbsp;</strong><code><strong>Pattern</strong></code><strong>&nbsp;class):&nbsp;</strong><code>Pattern.compile(&quot;regex_pattern&quot;)</code></li> </ul> <p><strong>Languages that typically do not use slashes around regex:</strong></p> <ul> <li><strong>C# Example:</strong>&nbsp;<code>Regex.Match(input_string, &quot;regex_pattern&quot;)</code></li> <li><strong>C++ Example (with the&nbsp;</strong><code><strong>&lt;regex&gt;</strong></code><strong>&nbsp;library)</strong>:&nbsp;<code>std::regex regex_pattern(&quot;regex_pattern&quot;);</code></li> <li><strong>C Example (using the POSIX regex library):</strong>&nbsp;<code>regcomp(&amp;regex_pattern, &quot;regex_pattern&quot;, 0)</code></li> <li><strong>Swift Example (with the&nbsp;</strong><code><strong>NSRegularExpression</strong></code><strong>&nbsp;class):</strong>&nbsp;<code>NSRegularExpression(pattern: &quot;regex_pattern&quot;, options: [])</code></li> </ul> <p>So, at this point you could get the idea, but and the &quot;i&quot; at the final of the regex? That&#39;s a&nbsp;<strong>flag,&nbsp;</strong>in this case it represents &quot;insensitive case&quot;, meaning that the regex must ignore the difference between Upper and Lower case. Although you can use this pattern to represent flags in JavaScript, you must be aware to use flags this way in other languages or technologies!</p> <p>If you understand everything above, now, you need to learn about specific symbols that you can commonly use in your regex, when they are together they are usually called&nbsp;<strong>Regex Pattern</strong>. I will separate them in categories to help you</p> <p><a href="https://medium.com/@lucasjuansomm/mastering-regular-expressions-a-comprehensive-guide-for-effective-pattern-matching-extracting-and-8e72fdca6e03">Click Here</a></p>