- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The simple definition of the regular expression is that it is a sequence of the characters, also called Regex or RegExp. It can be a small or complex sequence.
Let’s understand the need for regular expression by the example in the below section.
Why should we use regular expressions?
Suppose that you are developing the application and you need to take the users' email from the client-side. Now, users can enter anything in the input field, submit the form, and can generate a spam record to your database. As a programmer, you can take care of this and validate the email before the user submits the form. Also, you have seen it on the many websites that, if you make the syntax error in the email and submit, they will display a message like “image is not valid.”
To validate the input email, one way is to add too many if-else conditions to your code or use the regular expression to match the pattern in the string. By using the regular expression, we can create various patterns and replace too many if-else conditions with a single regular expression.
Users can see the regular expression below, to validate the email.
As a wise programmer, you shouldn’t be afraid of seeing the above expression. We will look into the deep to understand how regular expression works and simplify the use of regular expression.
Uses can follow the below syntax for the regular expressions.
Pattern in regular expression
There are some basic patterns to create regular expressions. Once you learn the basic pattern, you can create the advanced patterns for the regular expressions.
Let’s look at the basic patterns one by one.
[ a - z ] − It allows the user to match any character between the range of the characters given in brackets. Also, we can add a range for uppercase latter and other characters.
[ 0 -9 ] − It allows the user to match any digit between the range of the digits given in the brackets.
( a | b ) − It allows to find any alternative string separated with ‘|.’ For example, it finds either for a or b in the string.
\d − It accepts the digits only.
\s − It accepts the white space only.
\b − It finds the match at the starting or end of the word. For example, \bword, to match at the starting of the word, and word\b, to match the end of the word.
a+ − It matches with the string which contains at least a single ‘a’.
a* − It matches with the string which contains 0 to the Infinite number of ‘a’.
a? − It matches with the string that contains the zero or single occurrence of the character ‘a’.
Modifiers in Regular expressions
There are total three modifiers in regular expressions. Let’s take a look at one by one.
m − In simple words, it matches the multiline. Rather than stopping the match at the end of the string, it stops to match against the newline boundary.
i − When we set the modifier i, it performs the case-insensitive matching, which means it matches with the string without considering that character is in uppercase or lowercase.
g − It matches patterns globally. Otherwise, matching stops at the first occurrence of the pattern. When we set flag ‘g,’ it will match all the patterns.
In the below example, we have created the basic regular expression, and we will match the pattern in the particular string. Also, we will use the ‘i’ as a modifier to match case-insensitive.
In the above output, users can see that ‘Website’ is matched at the index 20.
In this tutorial, we have learned the basics of the regular expression. We have seen how patterns and modifiers are used in the regular expressions. Now, users can easily understand the regular expression, and with a small practice, they can create a regular expression independently.
- How to use regular expressions with TestNG?
- Validate the ZIP code with Java Regular expressions
- Validate Phone with Java Regular Expressions
- Explain the meanings of some of the regular expressions.
- Search and Replace with Java regular expressions
- Java Regular Expressions Tutorial
- What is Regular Expressions?