<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Felix</title>
    <description>The latest articles on DEV Community by Felix (@bachnxhedspi).</description>
    <link>https://dev.to/bachnxhedspi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F88521%2F0f1029d0-e35c-4558-8276-10f42744a3ca.jpeg</url>
      <title>DEV Community: Felix</title>
      <link>https://dev.to/bachnxhedspi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bachnxhedspi"/>
    <language>en</language>
    <item>
      <title>VerbalExpressions - RegularExpression made easy</title>
      <dc:creator>Felix</dc:creator>
      <pubDate>Wed, 15 Aug 2018 01:55:38 +0000</pubDate>
      <link>https://dev.to/bachnxhedspi/verbalexpressions---regularexpression-made-easy-27a8</link>
      <guid>https://dev.to/bachnxhedspi/verbalexpressions---regularexpression-made-easy-27a8</guid>
      <description>

&lt;p&gt;When you start learning a new programming language, maybe you had been learning follow those steps: variable, assignment, string, operators... One major theme you need to focus is &lt;code&gt;string operations&lt;/code&gt;. Fox example: get first name from fullname, find and censor all mobile numbers in message,...&lt;/p&gt;

&lt;p&gt;Along the operation we usually need to process some common procedure. One repeated procedure is finding a substring and implement some operations over the substring. Maybe you had done like something like this in the very beginning of your learning path.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int checkMatchStubPattern(char* string) {
    for(int i = 0; i &amp;lt; strlen(string); i++) {
        // logic for checking string pattern
        ...
    }
    return ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Not a wrong way, but a time consuming. You must change the checking logic in every case. More code, more bug and of course hard for maintainance. Luckily, &lt;code&gt;Regular Expression - Regex&lt;/code&gt; come as a hero to solve those kind of problems: find, input validation... As a confirmation for the usage of &lt;code&gt;Regex&lt;/code&gt;, every programming language supports &lt;code&gt;Regex&lt;/code&gt; for string operations.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Regular expression is a sequence of characters that define a search pattern. This pattern is then used by string searching algorithms for "find" or "find and replace" operations on strings, or for input validation. (Source: Wikipedia)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;Regex&lt;/code&gt; is an efficient tool to solve that problem; but it comes at a price. It is really hard to read and understand (but &lt;em&gt;not hard to learn&lt;/em&gt;). First try to read the example below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1B199waU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/nMveuD7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1B199waU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/nMveuD7.png" alt="Check whether a string is a valid url or not"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because of complicated syntax, it is very hard to read and understand &lt;code&gt;Regex&lt;/code&gt;. Furthermore, you seems not to work with &lt;code&gt;Regex&lt;/code&gt; too often. ROI (return-on-investment) is too low; almost common &lt;code&gt;Regex&lt;/code&gt; you need to use can be found on the internet (password, url, IP address,...). Are you willing to spend some weeks for learning something that you only use 4 or 5 times a year? Or just skimming over some sites for the result in around 5 minutes? That way of thinking make developers tend to google some &lt;code&gt;Regex&lt;/code&gt; and modify to fit into their needs. Sometime it can cost some hours to a whole day for the repetition process of searching - modifying...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mOFqjjfS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/j3G9xyP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mOFqjjfS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/j3G9xyP.png" alt="In every angel a demon hides..."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Regex&lt;/code&gt; solve the &lt;code&gt;string operation&lt;/code&gt; problem; but how about the problem of &lt;code&gt;Regex&lt;/code&gt;? Fortunately, it can be solved with &lt;a href="http://verbalexpressions.github.io/"&gt;&lt;code&gt;Verbal Expressions&lt;/code&gt;&lt;/a&gt;. Try to look at this example.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;VerEx&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;startOfLine&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'http'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;maybe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'s'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'://'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;anythingBut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;endOfLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I hope that you will not be frustrated after reading this example. The &lt;code&gt;Verbal Expressions&lt;/code&gt; above is defined following this rule:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The URL must start with either "http" or "https".&lt;/li&gt;
&lt;li&gt;The URL must then have "://".&lt;/li&gt;
&lt;li&gt;The URL can have anything following "://", as long as is not a space.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The generated &lt;code&gt;Regex&lt;/code&gt; from the above code is: &lt;code&gt;/^(?:http)(?:s)?(?:\:\/\/)(?:[^ ]*)$/&lt;/code&gt;. A bit diffenret but the functional is the same. You can find the implementation of &lt;code&gt;Verbal Expressions&lt;/code&gt; in several languages &lt;a href="http://verbalexpressions.github.io/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;VerbalExpressions solve the biggest probelem of &lt;code&gt;Regex&lt;/code&gt;. It is readable and easy-to-understand regular expressions. In my opinion, the transition from &lt;code&gt;Regex&lt;/code&gt; to &lt;code&gt;VerbalExpressions&lt;/code&gt; is great as the movement from &lt;code&gt;SQL&lt;/code&gt; to &lt;code&gt;ORM&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;Anyways, &lt;code&gt;VerbalExpressions&lt;/code&gt; still have some drawbacks. You need to install a new library to your project, sometimes it is quite painful (e.g. you client, manager... don't think it's neccessary). In that case, you can go to &lt;a href="https://verbalregex.com/"&gt;VerbalRegex&lt;/a&gt;; write the code and it will generate the &lt;code&gt;Regex&lt;/code&gt; for you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d0n2eqkX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/w0uMzYE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d0n2eqkX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/w0uMzYE.png" alt="Online tool for generating Regex"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try this tool by accessing &lt;a href="https://verbalregex.com/"&gt;verbalregex.com&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;VerbalExpressions&lt;/code&gt; is not a replacement of &lt;code&gt;Regex&lt;/code&gt;; but an easy way to write readable &lt;code&gt;Regex&lt;/code&gt;. It can ease the pain of &lt;code&gt;Regex&lt;/code&gt;, and actually make writing expressions fun again. But keep in mind that &lt;code&gt;Regex&lt;/code&gt; still seems to be the best choice in some complicated cases.&lt;/p&gt;


</description>
      <category>regex</category>
      <category>regularexpressions</category>
    </item>
  </channel>
</rss>
