Introduction to regular expression
Almost all the programming languages or databases or command lines uses the similar types of the syntax of regular expressions and the regular expressions which we will learn in this particular course will be compatible with all the programming language or database or command lines. Of course, minor modifications are required as per the language but they are very minor changes. So the syntax of regular expressions remains almost same in all the programming languages with little modification here and there.
What are Regular Expressions?
Regular Expressions Sequence of characters that defines a search pattern, it helps to validate a series of characters for matching. So, regular expression basically helps to describe the complex pattern in the text. We can use this to search a particular string, pattern, replace text, data.
Regular expressions are also called Regex or RegexP.
A sequence of characters that defines a search pattern.
What is the use of Regular Expressions?
- Can be used to validate a form.
- To extract a part of the file.
- To clean up a string.
- Used in database or command line as well.
- And used for other multiple purposes and list is on and on
Why learn regular Expressions?
Regular Expressions are used in most of the modern programming language like JAVA, HTML forms, JAVA scripts, python, PHP, Pearl etc. Suppose you have designed a particular Sign-up or a login page in a particular web site using any programming languages.
For example:
If you have used PHP or JavaScript or Python or even a simple external form then we can validate the input which user has provided so we can also accept the user input or we can reject the user input based on the validation performed by our regular expression.
For example, I give the first name and last name as Sandeep Kumar, and username as skljijkkj& and random and have given the password. when we click on next
it says sorry, only letters(a-z), numbers (0-9) and periods () are allowed. So this is the validation on input that I have provided. Using Regular Expression we can do the same thing. We can validate the data which is provided by a particular user and based on the validation of the product or regular expression a particular warning can be displayed to the user.
**We can also use a regular expression to extract certain parts of the strings which are also referred to as sub-string.
We have to read multiple files say 50 to 100 and each of these files contains several of thousands of url websites or multiple of phone no, date of birth etc.
If we try to do same work manually we can’t do it, I mean we can do it but it will be not effective as a regular expression. Because it will be time-consuming and it is an inefficient matter the file will not be a formatted file as well and the file can have special fields which might be separated by a comma or tabs or space and so on. In this case, it is best to use regular expressions to describe the search pattern.
**We can also use regular expressions for a lot of other purposes including database or command line as well.
How to write Regular Expressions?
This is one example of a simple regex expression. Actually, it is not simple.
For those who do not know anything about regular expression but once we go further in
this tutorial you’ll see that this is a pretty simple regular expression and a simple regular expression is used to match the word regular expression Regex or RegexP…
So if you just go to this particular website that is a https://regex101.com/
Here we have written the text pattern. (^reg(exp?|ular expressions?)
we will discuss this particular website in upcoming topics but if you notice
here I’ve written the pattern here.
If you see here it has 9 matched particular strings. It has 9 matches so it can match regular expressions in a smaller case, mixed case, an entire capital letters.
Here you see that it is matching the regular expression. There is no s after this n (paste screenshot of a regular expression.)
It can also match Regex in small case and Regex in mixed case and is also able to match this Regexp in mixed case and in a smaller case, capital as well.
And of course, it will also match with XP in the capital as well.
For example:
if I just write RegexP it has marked this as well but this will not match in Java or HTML or this particular text string.
We use particular Regex expression to match these particular strings.
For example, if you just write RegexP it will mark this as well but this will not match in Java or HTML or this particular text string.
This tutorial is all about how we can code regular expressions which is a sort of universal regex expressions that can be used in any language.
We will start from the basic and then we’ll move to some more advanced topics as well.