Regular Expression in simple terms is a special sequence of symbols and alphanumeric characters that defines a search pattern.
Java Regex can be defined as an API which is used for searching and manipulating (like replacing the matched word with some other word) strings from a given text which matches the pattern defined by the API.
Java provides a package known as java.util.regex
for pattern matching with regular expressions. This package consists of three main classes
Matcher class’ job is to interpret the pattern defined by pattern class and is used to perform matching functions on the given character sequence.
Some functions in the matcher class are:
true
else false
.true
else false
.Pattern class job is to compile the given regular expression which is used to define the pattern for a regex engine. This class accepts the pattern input provided by the user and compiles it so that matcher class can perform its tasks. One of the functions in Pattern class is
It simply is used to find errors in regular expression patterns which are provided by the user.
import java.util.regex.*;
public class reg {
Public static void main(String[] args) {
String email = "abcd99@gmail.com";
// Pattern for email validation with constraints
String pat = " ^ [a - zA - Z0 - 9_ ! #$ % &'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$";
Pattern p = Pattern.compile(pat); // Pattern Object
Matcher m = p.matcher(email); // Matcher Object
System.out.println(m.matches());
}
}
Output: Prints true if the email is valid and false for invalid, here it will print true
Regex quantifiers are used in regular expressions to define how often an element can occur in the strings which the user is searching for. Some examples are
*
- it symbolizes that occurrences of the element should be zero or more times+
- it symbolizes that occurrences of the element should be one or more times?
- it symbolizes occurrence of the element should be zero or one.{X}
- it symbolizes occurrences of the element should be X number of times{X, Y}
- it symbolizes occurrences of the element should be between X
and Y
number of times{X,}
- it symbolizes occurrences of the element should be X
or more than X
Java Character Class has predefined sets of expressions which can be directly used as regex expressions and can be used to create our required pattern.
[xyz]
- checks occurrence of x, y or z[^xyz]
- checks occurrence of any character other than x, y or z[a-zA-Z]
- checks occurrence of characters from a-z or A-Z[a-z&&[xyz]]
- checks occurrence of characters from a-z with xor y or z[a-z&&[^xy]]
- checks occurrence of characters from a-z except x or y or z[a-z&&[^m-p]]
- checks occurrence of characters from a-z and should not have characters m through pimport java.util.regex. * ;
public class Reg {
public static void main(String[] args) {
Pattern p = Pattern.compile("[abc]{3}");
Matcher m = p.matcher("bcc");
Matcher ma = p.matcher("acz");
System.out.println(m.matches());
System.out.println(ma.matches());
}
}
Output:
true
false
This program will only print true under the following conditions:
These are ordinary characters which have special meaning in computer programs and are used for making regular expressions.
Some examples are:
import java.util.regex. * ;
public class Reg {
public static void main(String[] args) {
Pattern p = Pattern.compile("\\D{3}");
Matcher m = p.matcher("123");
Matcher ma = p.matcher("aaa");
System.out.println(m.matches());
System.out.println(ma.matches());
}
}
Output:
false
true
Help us improve this content by editing this page on GitHub