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`.
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
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 -
Let’s understand what these characters actually mean -
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
Let’s crack this regular expression, it looks a bit long, right? Don’t worry we’ll understand it bit-by-bit,
Check if Email is valid
This is the best use of regex as it allows us to put validation in a form.
Above is a basic regular expression in order to validate an email address -
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` -
Above is a basic regular expression in order to validate an credit card -
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.