Technoarch Softwares - What is  Regular Expression?

What is Regular Expression?

A Regex or Regular Expression is a sequence of characters which is used to search patterns in a given string. It is mostly used in projects where the requirement involves dealing with extracting information from text such as codes, files, log, spreadsheet and can also be used in the text validation.

A short example of what Regex really is -

Now you want to search for all the numbers which have the following format  `xxx-xxx-xxxx`.

\d\d\d-\d\d\d-\d\d\d\d


Let’s decode this weird looking regex -

`\d` is known as a special sequence, which returns the pattern where the string contains numbers from 0 to 9.

That was easy right, let’s roll into more of such weird stuff.

Two Types of Regular Expression

Literal Characters 

These are the most basic regex which merely consists of a single literal character for example if you search for a, you will find words which have a in it, it basically depends on what you typed in.

Note - One thing to keep in mind, it will not search for C if you typed in c, as it is case sensitive.

Meta Characters or Special Character

These characters help in the search for more than just the literal piece of text. It gives the special meaning to the literal characters by transforming them into more complex characters which have more than one meaning depending on the context.

Let’s dive into the understanding of Meta Characters with the help of numerous examples -

  • Match all Numbers

    Now we’ll start off with a simple regex which will help us in finding the pattern that only contains a number line -

    ^[0-9]+$

     

    Let’s understand what these characters actually mean -

    ^ - Represents starting position of string or line
    [0-9] - It matches all numbers between 0 and 9
    + - It matches one or more instance of the preceding expression
    $ - Represents the end of line

     

    Note - at the place of [0-9], one can also use `\d` which will do the same thing

    The important point is without much modification in the regex, it can be used in any of the programming languages.

  •  Match all Dates

     Now let’s match all the date which is of format DD/MM/YYYY or DD-MM-YYYY

    ^\d{2}[\/\-]\d{2}[\/\-]\d{4}$


    Let’s crack this regular expression, it looks a bit long, right? Don’t worry we’ll understand it bit-by-bit,

    \d{2} - Matches any digit between 00 and 99

    [\/\-] - It matches the separator with either / or -

    \d{4} - Matches any digit between 0000 and 9999 

     

  • Check if Email is valid

    This is the best use of regex as it allows us to put validation in a form.

    ^[\w.]+@[\w]+.\w{2,6}$

    Above is a basic regular expression in order to validate an email address -

    [\w.] - \w matches any word character (a-z , 0-9 digits and underscore character) either with a dot or not

    -  Matches one or more occurrences

     @ - Match the @ symbol after which is must

    .\w{2,6} - word character 2 to 6 times with a preceding dot

     

  • Credit card validation  

    As we all know that there is not one but many different kind of credit cards which follows different format, the coming example will make you aware of a credit card validation which has a basic format `XXXX-XXXX-XXXX-XXXX` -

    \d{4}[-, ]?\d{4}[-, ]?\d{4}[-, ]\d{4}

     

    Above is a basic regular expression in order to validate an credit card -

     \d{4} - Matches any 4 digits between 0000 and  9999

    [-, ] - Matches separator with hyphen ` -`, comma `,` or space

    ? - preceding characters is optional

     

     

Conclusion

Great! you have reached the end of this blog and I am guessing these weird looking characters are not so weird for you now, as you must have become familiar with lots of special characters and now you can give it a go to try your hand in building your own regex.

I have introduced a basic usage of what regular expression can really do, by giving you a gist of using it in order to understand the characters through real life examples. 

Feel free to comment down below for any suggestion or query.

4 Comments:

  1. 555

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  2. 555

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  3. 555

    Leave a Reply

    Your email address will not be published. Required fields are marked *

  4. 555

    Leave a Reply

    Your email address will not be published. Required fields are marked *

Leave a Comments

Your email address will not be published. Required fields are marked *