From 6d0040d14d4a6434bd0f1cd3d731e063bc6e83aa Mon Sep 17 00:00:00 2001 From: Aakash Panchal <51417248+Aakash-Panchal27@users.noreply.github.com> Date: Sat, 7 Mar 2020 19:42:41 +0530 Subject: [PATCH] Update and rename Intro_Basic.md to Intro_Basic.html --- Akash Articles/RegEx/Intro_Basic.html | 155 ++++++++++++++++++++++++++ Akash Articles/RegEx/Intro_Basic.md | 1 - 2 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 Akash Articles/RegEx/Intro_Basic.html delete mode 100644 Akash Articles/RegEx/Intro_Basic.md diff --git a/Akash Articles/RegEx/Intro_Basic.html b/Akash Articles/RegEx/Intro_Basic.html new file mode 100644 index 0000000..3176025 --- /dev/null +++ b/Akash Articles/RegEx/Intro_Basic.html @@ -0,0 +1,155 @@ + + + + + + + + + + + +

Regular Expression (RegEx)

+ +

While filling online forms, haven't you come across errors like "Please enter valid email address" or "Please enter valid phone number".

+ +

Annoying as they may be, there's a lot of black magic that the computer does before it determines that, the details you've entered are incorrect.

+ +

Can you think out, what is that black magic? If you are familiar with algorithms, then you will say that we can write an algorithm for the same.

+ +

Yes, we can write an algorithm to verify different things, but we have a standard tool designed for similar kinds of purposes.

+ +

It is Regular Expression. We call it RegEx for short. RegEx makes our work a lot easier. Let's see some basic examples where RegEx becomes handy.

+ +

Suppose, you are in search of an averge price of a particular product on amazon. The following regular expression will find you any price(ex. $12, $75.50) on the webpage: \$([0-9]+)\.([0-9]+). In the Image below, yellowish part shows the matched prices.

+ +

enter image description here

+ +

Quite interesting!

+ +

Let's look at another example. You have a long list of documents with different kinds of extensions. You are particularly looking for data files having .dat extension.

+ +

^.*\.dat$ is a regular expression which represents a set of string ending with .dat. Regular expression is a standardized way to encode such patterns.

+ +

Below in the image, you can see that all three files having .dat extension are extracted from the list of five files.

+ +

enter image description here

+ +

Well. What does the name Regular Expression(RegEx) represent? Regular Expression represents the sequence of characters that defines a regular search pattern.

+ +

RegEx is a standardized tool to do the following works:

+ +
    +
  1. Find and verify patterns in a string.
  2. + +
  3. Extract particular data present in the text.
  4. + +
  5. Replace, split and rearrange particular parts of a string.
  6. +
+ +

We are going to look at all the three things above.

+ +

Let's begin the journey with RegEx!

+ +

Note:

+ +
    +
  1. Alpha-numeric character belongs to anyone of the $0-9,A-Z,a-z$ ranges.
  2. + +
  3. String is a sequence of characters and substring is a contiguous part of a string.
  4. +
+ +

Simple Alpha-numeric character matching

+ +

Simple matching of a specific word can be done as the following:

+ +
+ +
+ +

As you can see it matches "Reg" in the text. Similarly, what will be the match for "Ex" in the same text above?

+ +
+ +
+ +

Do you notice anything? It is a case sensitive.

+ +

Implementation

+ +

Most of the programming languages have libraries for RegEx. They have almost similar kind of syntax. Here, we will see how to implement it in Javascript.

+ +

Below is a basic code in Javascript, showing how to implement regex. The patterns are written in /_____/g. Where g is a modifier, which is used to find all matches rather than stopping at the first match.

+ +

The function exec returns null, if there is no match and match data otherwise.

+ +
// Main text (string) in which we are finding
+// Patterns
+var str = "RegEx stands for Regular Expression!";
+
+// Pattern string
+var pattern = /Reg/g;
+
+// This will print all the data of matches
+// across the whole string
+while(result = pattern.exec(str))
+{
+    console.log(result); // printing
+}
+
+// This will be the output
+/*
+[
+  'Reg',
+  index: 0,
+  input: 'RegEx stands for Regular Expression!',
+  groups: undefined
+]
+[
+  'Reg',
+  index: 17,
+  input: 'RegEx stands for Regular Expression!',
+  groups: undefined
+]
+*/
+
+ +

Note: Groups in the above output is a RegEx concept.

+ + + + diff --git a/Akash Articles/RegEx/Intro_Basic.md b/Akash Articles/RegEx/Intro_Basic.md deleted file mode 100644 index 8b13789..0000000 --- a/Akash Articles/RegEx/Intro_Basic.md +++ /dev/null @@ -1 +0,0 @@ -