<?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>Forem: Noor Sheikh</title>
    <description>The latest articles on Forem by Noor Sheikh (@noorsheikh).</description>
    <link>https://forem.com/noorsheikh</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%2F353275%2Fe40d268d-2191-4fc8-a8cb-d26e8f466f3f.jpeg</url>
      <title>Forem: Noor Sheikh</title>
      <link>https://forem.com/noorsheikh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/noorsheikh"/>
    <language>en</language>
    <item>
      <title>An Overview of Regex Expressions</title>
      <dc:creator>Noor Sheikh</dc:creator>
      <pubDate>Sun, 05 Apr 2020 04:02:30 +0000</pubDate>
      <link>https://forem.com/noorsheikh/an-overview-of-regex-expressions-5cj4</link>
      <guid>https://forem.com/noorsheikh/an-overview-of-regex-expressions-5cj4</guid>
      <description>&lt;p&gt;In this post, I am going to have a quick overview of regex expressions. The review is based on my learning outcomes from one of my recent MS course. &lt;/p&gt;

&lt;p&gt;A definition of regular expression from the internet.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;a href="https://www.regular-expressions.info/"&gt;regular expression&lt;/a&gt; (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as &lt;em&gt;.txt to find all text files in a file manager. The regex equivalent is ^.&lt;/em&gt;.txt$.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Let's start with an example regex expression
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;^[A-Za-z]+[._-]?[A-Za-z0-9]*[@][A-Za-z0-9]{2,}\.[a-z]{2,6}$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Any guess what the above regex expression represent? If your guess is an email address then you are right. The above regex expression represents a valid pattern for an email address. Although, this might not be a fully valid email address pattern let's use it as an example here.&lt;/p&gt;

&lt;p&gt;Below is the valid matching email address for above regex expression:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;firstlast@domain.com&lt;/code&gt;&lt;br&gt;
&lt;code&gt;first.last@domain.net&lt;/code&gt;&lt;br&gt;
&lt;code&gt;first_last@domain.us&lt;/code&gt;&lt;br&gt;
&lt;code&gt;first-last12@longdomain.online&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="//regexr.com/51ron"&gt;Check it out here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's break down the above regex expression and compare it with the result.
&lt;/h2&gt;

&lt;p&gt;First of all, every regex expressions begin with &lt;code&gt;^&lt;/code&gt; caret and end with a &lt;code&gt;$&lt;/code&gt; dollar sign. These two signs indicate the starting and end of a regex expression.&lt;/p&gt;

&lt;p&gt;Now, let's extract the first portion of the email before the &lt;code&gt;@&lt;/code&gt; sign, which can also be named as the username.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[A-Za-z]+[._-]?[A-Za-z0-9]*&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's break it further into three parts.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;[A-Za-z]+&lt;/code&gt; this pattern represents case insensitive one or more &lt;code&gt;+&lt;/code&gt; letter(s) from a to z &lt;code&gt;A-Za-z&lt;/code&gt;. An example is &lt;code&gt;Firstlast&lt;/code&gt; and &lt;code&gt;first&lt;/code&gt; from the above email.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[._-]?&lt;/code&gt; this part of the pattern represents an optional &lt;code&gt;?&lt;/code&gt; special character of type &lt;code&gt;.&lt;/code&gt; period, &lt;code&gt;_&lt;/code&gt; underscore or &lt;code&gt;-&lt;/code&gt; dash (hyphen) as seen in the example emails above.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[A-Za-z0-9]*&lt;/code&gt; finally this part of the pattern represents zero or more &lt;code&gt;*&lt;/code&gt; characters of type upper or lower case &lt;code&gt;A-Za-z&lt;/code&gt; letter(s) or digit(s) from zero to 9 &lt;code&gt;0-9&lt;/code&gt; after one of the special characters from &lt;code&gt;._-&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;[@]&lt;/code&gt; donates the at sign in the email.&lt;/p&gt;

&lt;p&gt;Finally, the last portion of an email is the domain name of the provider and it is donated as below in regex expression.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[A-Za-z0-9]{2,}\.[a-z]{2,6}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's break it further into three parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;[A-Za-z0-9]{2,}&lt;/code&gt; this part of the pattern represents 2 or more &lt;code&gt;{2,}&lt;/code&gt; characters of type upper and lower case letter(s) from a to z &lt;code&gt;A-Za-z&lt;/code&gt; and digit(s) from 0 to 9 &lt;code&gt;0-9&lt;/code&gt;. An example is &lt;code&gt;domain.com&lt;/code&gt; from the above list of emails.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\.&lt;/code&gt; this part represents the period used in the domain name part of the email. Note: &lt;code&gt;\&lt;/code&gt; is used for escaping.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[a-z]{2,6}&lt;/code&gt; this part represents 2 to 6 &lt;code&gt;{2,6}&lt;/code&gt; characters from a to z &lt;code&gt;a-z&lt;/code&gt; of the last portion of email after the period sign.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Explanation of regex characters:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;^&lt;/code&gt;: indicates the start of regex expression.&lt;br&gt;
Example Usage: &lt;code&gt;^.$&lt;/code&gt; (it returns any character &lt;code&gt;ABCabc123!#@$#%$#%&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$&lt;/code&gt;: indicates the end of regex expression.&lt;br&gt;
Example Usage: &lt;code&gt;^.$&lt;/code&gt; (it returns any character &lt;code&gt;ABCabc123!#@$#%$#%&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\&lt;/code&gt;: indicates escaping in regex expression.&lt;br&gt;
Example usage: &lt;code&gt;[a-z]\.[a-z]&lt;/code&gt; (it escape period between characters &lt;code&gt;abc.def&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;+&lt;/code&gt;: indicates one or more characters in a pattern.&lt;br&gt;
Example usage: &lt;code&gt;[a-z]+&lt;/code&gt; (it returns one or more lowercase characters &lt;code&gt;abcdef&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;*&lt;/code&gt;: indicates zero or more characters in a pattern.&lt;br&gt;
Example usage: &lt;code&gt;[a-z]+[0-9]*&lt;/code&gt; (it indicates optional digit(s) at the end of text &lt;code&gt;abcdef123&lt;/code&gt; and &lt;code&gt;abcdef&lt;/code&gt; both are valid results)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;?&lt;/code&gt;: indicates zero or one character in a pattern.&lt;br&gt;
Example Usage: &lt;code&gt;0?[1-9]&lt;/code&gt; (it makes zero optional at the begging of single-digit &lt;code&gt;01&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt; both are valid results)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;|&lt;/code&gt;: indicates or/alternative in a pattern.&lt;br&gt;
Example Usage: &lt;code&gt;(cat|dog)&lt;/code&gt; (the valid result of expression is either &lt;code&gt;cat&lt;/code&gt; or &lt;code&gt;dog&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[]&lt;/code&gt;: indicates matching of values in a pattern.&lt;br&gt;
Example Usage: &lt;code&gt;ca[tr]&lt;/code&gt; (the valid result of the expression is &lt;code&gt;ca&lt;/code&gt; followed by one of the values inside the brackets, &lt;code&gt;car&lt;/code&gt; or &lt;code&gt;cat&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;()&lt;/code&gt;: indicates the grouping of values in a pattern.&lt;br&gt;
Example Usage: &lt;code&gt;(1|2|3)&lt;/code&gt; (the valid result of the expression is on of the values inside the group separated by the pip sign, &lt;code&gt;1&lt;/code&gt; or &lt;code&gt;2&lt;/code&gt; or &lt;code&gt;3&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;A-Z&lt;/code&gt;: indicates upper case letters from a to z.&lt;br&gt;
Example Usage: &lt;code&gt;^[A-Z][a-z]&lt;/code&gt; (it capitalize first letter for first name &lt;code&gt;John&lt;/code&gt;, &lt;code&gt;Mark&lt;/code&gt; etc)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;a-z&lt;/code&gt;: indicates lower case letters from a to z.&lt;br&gt;
Example Usage: &lt;code&gt;^[A-Z][a-z]&lt;/code&gt; &lt;code&gt;John&lt;/code&gt;, &lt;code&gt;Mark&lt;/code&gt; etc.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0-9&lt;/code&gt;: indicates digits from 0 to 9.&lt;br&gt;
Example Usage: &lt;code&gt;^[2-9][0-9]{3} [1-9][0-9]{2}-[0-9]{4}&lt;/code&gt; = &lt;code&gt;340 597-1234&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt; or &lt;code&gt;\s&lt;/code&gt;: indicates white space in a pattern.&lt;br&gt;
Example Usage: &lt;code&gt;[A-Z][a-z]\s[A-Z][a-z]&lt;/code&gt; (it inserts space between first name and last name &lt;code&gt;Steve Jobs&lt;/code&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus
&lt;/h2&gt;

&lt;h3&gt;
  
  
  US Phone Number &lt;a href="//regexr.com/51rrb"&gt;Try it here&lt;/a&gt;
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Pattern
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;^[2-9][0-9]{2}\s[1-9][0-9]{2}-[0-9]{4}$&lt;/code&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Explanation
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;^[2-9][0-9]{2}&lt;/code&gt;: Start the expression, add initial digit between 2 and 9 followed by two additional digits between 0 and 9.&lt;br&gt;
&lt;code&gt;\s&lt;/code&gt;: Represent a white space&lt;br&gt;
&lt;code&gt;[1-9][0-9]{2}&lt;/code&gt;: After white space, add a digit between 1 and 9 followed by two additional digits between 0 and 9.&lt;br&gt;
&lt;code&gt;-[0-9]{4}$&lt;/code&gt;: Add a hyphen followed by four additional digits between 0 and 9 and mark the end of the expression.&lt;/p&gt;

&lt;h5&gt;
  
  
  Matching Phone Number
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;234 123-4567&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Social Security Number &lt;a href="//regexr.com/51rrn"&gt;Try it here&lt;/a&gt;
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Pattern
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;^[0-9]{3}-[0-9]{2}-[0-9]{4}$&lt;/code&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Explanation
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;^[0-9]{3}-&lt;/code&gt;: Start the expression and add three digits between 0 and 9 followed by a hyphen.&lt;br&gt;
&lt;code&gt;[0-9]{2}-&lt;/code&gt;: Add two digits between 0 and 9 followed by a hyphen.&lt;br&gt;
&lt;code&gt;0-9]{4}$&lt;/code&gt;: Add four digits between 0 and 9 and mark the end of the expression.&lt;/p&gt;

&lt;h5&gt;
  
  
  Matching SSN
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;000-00-0000&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Street Address &lt;a href="//regexr.com/51rrq"&gt;Try it here&lt;/a&gt;
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Pattern
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;^[1-9][0-9]{3}\s[A-Z][a-z]+\s[A-Z](a-z.)?|[a-z]+$&lt;/code&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Explanation
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;^[1-9][0-9]{3}&lt;/code&gt;: Start the expression, add a digit between 1 and 9 followed by 3 more digits between 0 and 9.&lt;br&gt;
&lt;code&gt;\s&lt;/code&gt;: Add white space.&lt;br&gt;
&lt;code&gt;[A-Z][a-z]+&lt;/code&gt;: Add an upper case letter followed by one or more lower case letters.&lt;br&gt;
&lt;code&gt;\s&lt;/code&gt;: Add white space.&lt;br&gt;
&lt;code&gt;[A-Z](a-z.)?|[a-z]+$&lt;/code&gt;: Add an uppercase letter followed by either one letter and a period, or one or more lower case letters and mark the end of the expression.&lt;/p&gt;

&lt;h5&gt;
  
  
  Matching Street Address
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;1234 Sample Street&lt;/code&gt;&lt;br&gt;
&lt;code&gt;1234 Sample St.&lt;/code&gt;&lt;/p&gt;

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