Regular Expressions - not so regular!
Tags: regex, regularexpressions
Published: 2020-05-17 15:13:16

### Table Of Contents
* [Modifiers (or flags)](#mod)
* [Brackets](#brac)
* [Meta Characters](#meta)
* [Quantifiers](#quant)
* [Miscellaneous](#misce)
* [Real World applications](#real)
* [Summary](#sum)

##Introduction

Regular Expressions aka RegEx. or RegExp. are powerful tools widely used for pattern matching within strings. They are supported by almost all programming languages. However choice of language plays a key role in deciding the usage and syntax of the same. Here, I'll be using JavaScript. A Website <a href="https://regex101.com">regex101</a> will let you use regular expressions even without the knowledge of programming language.

###What does it look like?
{% jsfiddle https://jsfiddle.net/utkarsh48/d01mxacu/ js %}
This code will make sense as you proceed further.

##How to use regular expressions
It depends majorly upon the language. In JavaScript there are various functions for the same eg: <code>match()</code>, <code>test()</code> and <code>search()</code> are widely used. In MySQL keyword <code>LIKE</code>, <code>REGEXP</code> or operator <code>~</code> (aka post-fix operator) are used for searching.

{% jsfiddle https://jsfiddle.net/utkarsh48/uhzk8gL4/ js,result %}

![A meme](https://miro.medium.com/max/1232/1*1aInccdfvdlIx5WRxBk0Gw.jpeg)
<figcaption>Kneel before the god ;P by <a href="https://memegenerator.com">memegenerator</a></figcaption>


##Regular Expressions usually have
Quantifiers, Operators, Modifiers (or Flags).

###Modifiers <a name="mod"></a>
<table>
<tr><th>Modifier</th><th>Description</th></tr>
<tr><td>'g'</td><td>for global search i.e. It won't stop searching after first match and will return all the matches.</td></tr>
<tr><td>'i'</td><td>for case in-sensitive search</td></tr>
<tr><td>'m'</td><td>for multi-line search (the regexp should be surrounded by ^ and $)</td></tr>
</table>


###Brackets<a name="brac"></a>
<table>
<tr><th>Brackets</th><th>Description</th></tr>
<tr><td>[abc]</td><td>look for any one character between the square brackets(like an OR operator in programming languages)</td></tr>
<tr><td>[a-z]</td><td>includes all lower-case alphabets</td></tr>
<tr><td>[A-Z]</td><td>includes all upper-case alphabets</td></tr>
<tr><td>[A-z]</td><td>includes both upper-case and lower-case alphabets</td></tr>
<tr><td>[0-9]</td><td>includes all numerals</td></tr>
<tr><td>[^abc]</td><td>Will look for everything other than a,b,c (^ represents inverse here only in square-brackets otherwise it represent anchor operator)</td></tr>
<tr><td>(p|q)</td><td>It is known as grouping operator, it divides the expression within sub-groups. It also saves the index of subgroup starting from 1-9 (which is used by back-reference operator)</td></tr>
</table>

###Meta characters<a name="meta"></a>
<table>
<tr>
    <th>Meta-character</th>
    <th>Description</th>
  </tr>
  <tr>
    <td>.</td>
    <td>Finds a single character, except newline or line terminator</td>
  </tr>
  <tr>
    <td>\w</td>
    <td>Finds a word character it include [A-Za-z0-9_]</td>
  </tr>
  <tr>
    <td>\W</td>
    <td>Finds a non-word character</td>
  </tr>
  <tr>
    <td>\d</td>
    <td>Finds a digit</td>
  </tr>
  <tr>
    <td>\D</td>
    <td>Finds a non-digit character</td>
  </tr>
  <tr>
    <td>\s</td>
    <td>Finds a white-space character</td>
  </tr>
  <tr>
    <td>\S</td>
    <td>Finds a character that is not white-space</td>
  </tr>
  <tr>
    <td>\b</td>
    <td>Finds a match only at the beginning or end of a word, beginning like this: \bHI, end like this: HI\b</td>
  </tr>
  <tr>
    <td>\B</td>
    <td>Finds a match, where \b doesn't</td>
  </tr>
  <tr>
    <td>\n</td>
    <td>Finds a line feed character</td>
  </tr>
  <tr>
    <td>\f</td>
    <td>Finds a form feed character</td>
  </tr>
  <tr>
    <td>\r</td>
    <td>Finds a carriage return character</td>
  </tr>
  <tr>
    <td>\t</td>
    <td>Finds a tab character</td>
  </tr>
</table>

###Quantifiers<a name="quant"></a>
<table>
<tr><th>Quantifiers</th><th>Description (n here is anything you want to match)</th></tr>
<tr><td>n{2}</td><td>match only 2 instances of n</td></tr>
<tr><td>n{1,5}</td><td>match at least 1 at most 5 instances of n</td></tr>
<tr><td>n{1,}</td><td>match at least 1 instance of n</td></tr>
<tr><td>n+</td><td>Shorthand to match 1 or more instances of n</td></tr>
<tr><td>n*</td><td>Shorthand to match 0 or more instances of n</td></tr>
<tr><td>n?</td><td>Shorthand to match 0 or 1 instances of n</td></tr>
</table>

###Miscellaneous<a name="misce"></a>
![escape sequence](https://imgs.xkcd.com/comics/backslashes.png
)
<figcaption>
Meme on escape-sequence by <a href="https://xkcd.com">xkcd</a>
</figcaption>

'|' operator can be used to separate sub groups.
'\1' operator(aka back-reference operator) is used to refer to previous subgroup of that index 1 in this case.
There is no concatenation operator as such. Things written together are already concatenated.
Some symbols that are used in expressions can't be used directly. '\' (backslash) is used to escape-sequence.
Multiple bracket sets can be used like [a-z] and [0-9] can be combined in following way [a-z0-9] without '|' and space.
^ and $ are used at beginning and end respectively however when ^ is used as [^\w]+ it is used to invert the selection. In this case no word will be selected i.e. only symbols and white spaces will be selected.

{% jsfiddle https://jsfiddle.net/utkarsh48/uLwbz4km/ js,result %}



##Let's see some interesting examples
To validate an email, username and password
{% jsfiddle https://jsfiddle.net/utkarsh48/nvgeamhL/ js,result %}

![meme on regular expressions](http://www.quickmeme.com/img/0e/0efbe795dea9779f29d4ca4d6d470393606e2152b18b6dabdb2e5bf0c6a671e3.jpg)
<figcaption>Values are screened by RegExp. for security purpose. By <a href="https://quickmeme.com">Quickmeme</a></figcaption>


##Real-world application of RegEx.<a name="real"></a>

* __Forms:__ Usernames, Email addresses, passwords etc. are validated through RegEx. expressions. as seen above.

* __Search:__ RegEx. are used to search for files and within files. Sometimes replacement is followed by search.

* __Replace:__ With new updates in languages newer syntax come into play and sometimes older syntax are depreciated. Therefore changing each line of code would be a miserable. To ease the process there are programs that can convert syntax without damaging the functioning of original code.

* __Data Extraction:__ RegEx. are used in SQL (a language that makes queries to database) to look for records matching some criteria.

![saved the day](https://imgs.xkcd.com/comics/regular_expressions.png)
<figcaption>Saved the day. Source <a href="https://xkcd.com">Xkcd</a></figcaption>

###Summary<a name="sum"></a>
With Regular expression in your tool box, you can manipulate strings like pro!
Otherwise your life would be much more difficult than it need to be. Learn more at <a href="https://www.w3schools.com/jsref/jsref_obj_regexp.asp">W3school</a>
 <a href="http://web.mit.edu/gnu/doc/html/regex_3.html">or here</a>