This write-up presents a thorough understanding of the following concepts:
- Pattern Creation using Compile Method
- Pattern Matching using Matcher Method
- What are Flags in Regular Expressions
- What are Regular Expression Patterns
- What are MetaCharacters in Regular Expressions
- What are Quantifiers in Regular Expressions
- Practical Implementation of Regular Expressions in Java
So, let’s get started!
Pattern Creation using Compile() Method
In order to create a pattern, firstly we have to invoke the compile() method of the Pattern class and as a result, it will return a pattern object. The compile() method can take two parameters: first one for the pattern to be searched and the second one is optional and can be used to specify a flag.
What are Patterns in Regular Expressions
In the first parameter, the compile() method specifies a pattern to be searched. Some commonly used search patterns are listed below:
- [—] here “—” represents different characters, and the entire pattern i.e. [—] describes that find a character between the options provided within the brackets.
- [^—] describes finding a character that is not a part of the provided pattern.
- [0-9], describes finding a number between 0 to 9.
What are Flags in Regular Expressions
Flags are optional parameters to the regular expressions and can be used to specify their(regular expressions) searching behavior. For example, CASE_INSENSTIVE can be used to perform the search irrespective of the letter’s case, i.e. uppercase or lowercase.
Pattern Matching using Matcher() Method
The pattern returned by the compile() method will be interpreted by the matcher() method to perform the match operation over the String.
What are MetaCharacters in Regular Expressions
Java provides several Metacharacters that have some special meaning and are helpful in defining the search criteria:
MetaCharacters | Description |
\d | Used to find a digit |
\b | Used to find a match at the start or end of the word |
$ | Used to find the match at the end of the string |
^ | Used to find the match at the start of the string |
\s | Search for the white spaces |
| | Search for a match from multiple options that are separated with ‘|’ sign |
. | Used to match a single instance of a character |
What are Quantifiers in Regular Expressions
Quantifier specifies the number of occurrences to be matched, some commonly used quantifiers are listed below:
Quantifiers | Description |
A+ | A occurs at least one time |
A* | A occurs zero or more time |
A? | A occurs either zero time or one time |
A{n} | A occurs n number of times |
A{n,} | A occurs n times or more than n times |
A{x,y} | A occurs between the provided range i.e. A occurs at least x times but less than y times |
Practical Implementation of Regular Expressions in Java
Let’s implement the above-mentioned concepts in a practical scenario for a profound understanding.
Example
In the below code snippet, we searched for the word “linuxhint” in a sentence using a regular expression:
publicstaticvoidmain(String[] args) {
Pattern pat = Pattern.compile("LinuxHint", Pattern.CASE_INSENSITIVE);
Matcher match = pat.matcher("Welcome to linuxhint.com");
boolean found = match.find();
if (found) {
System.out.println("Match found successfully");
} else {
System.out.println("Match not found");
}
}
}
Initially, we created the object of the Pattern class, then we specify a word that we want to search and a flag “CASE_INSENSITIVE” within the compile() method. Next, we utilize the matcher() method to perform the match operation over the String.
The output authenticates that matches are found successfully regardless of case sensitivity.
Conclusion
The regular expressions are nothing but a sequence of characters that defines the search patterns. In java, regular expressions are used to search, edit, and manipulate a string. To create a pattern, first we have to invoke the compile() method of the Pattern class and consequently, it will return a pattern which will be interpreted by the matcher() method to perform the match operation over the String. Moreover, Java provides several Metacharacters that are helpful in defining the searching criteria and Quantifiers to specify the number of occurrences to be matched.