Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Data extraction or validation is an important aspect of every programming language. One of the most popular ways for data validation is by using regular expressions. Java uses these regular expressions to describe a pattern of characters. This article on Java Regex will list out the various methods of using expressions in the following sequence:
Let’s get started!
A Regular Expression is a sequence of characters that constructs a search pattern. When you search for data in a text, you can use this search pattern to describe what you are looking for.
A regular expression can be a single character or a more complicated pattern. It can be used for any type of text search and text replace operations. A Regex pattern consist of simple characters, such as /abc/, or a combination of simple and special characters, such as /ab*c/ or /example(d+).d*/.
The Java Regex is an API which is used to define a pattern for searching or manipulating Strings. It is widely used to define the constraint on Strings such as password and email validation.
There are different methods of using Java Regex. So let’s move ahead and have a look at the different expressions.
This class is used to perform match operations on a character sequence. Below table represents various methods of Matcher class.
Pattern Class is a compiled version of regular expression which is used to define the pattern for regex engine.
Method | Description |
---|---|
static Pattern compile(String regex) | It compiles the given regex and returns the instance of the Pattern |
Matcher matcher(CharSequence input) | It is used to create a matcher that matches the given input with the pattern |
static boolean matches(String regex, CharSequence input) | It works as a combination of compile and matcher methods. It compiles the regular expression and matches the given input with the pattern |
String[] split(CharSequence input) | Used to split the given input String around matches of a given pattern |
String pattern() | Helps to Return the regex pattern |
Related Learning: Java Pattern Programs
Now let’s take a small example to understand how to write a regular expression.
import java.util.regex.*; public class RegexExample{ public static void main (String[] args){ Pattern pattern = Pattern.compile(".xx."); Matcher matcher = pattern.matcher("AxxB"); System.out.println("String matches the given Regex - +matcher.matches()); } }
In this case, internally it uses Pattern and Matcher Java regex classes to do the processing but obviously, it reduces the code lines. Pattern class also contains matches method that takes regex and input String as argument and returns a boolean result after matching them. So the code works fine for matching input String with a Regular expression in Java. Hence the output will be true as shown below.
Output:true
Now let’s see a few more categories of Java Regular Expressions.
Below table represents the different character class combination.
Character Class | Description |
---|---|
[abc] | a, b, or c (simple class) |
[^abc] | Any character except a, b, or c (negation) |
[a-zA-Z] | a through z or A through Z, inclusive (range) |
[a-d[m-p]] | a through d, or m through p: [a-dm-p] (union) |
[a-z&&[def]] | d, e, or f (intersection) |
[a-z&&[^bc]] | a through z, except for b and c: [ad-z] (subtraction) |
[a-z&&[^m-p]] | a through z, and not m through p: [a-lq-z](subtraction) |
import java.util.regex.*; public class CharacterExample{ public static void main(String args[]){ //false (not x or y or z) System.out.println(Pattern.matches("[xyz]", "wbcd")); //true (among x or y or z) System.out.println(Pattern.matches("[xyz]", "x")); //false (x and y comes more than once) System.out.println(Pattern.matches("[xyz]", "xxyyyyyz")); } }
Discover how to create mobile apps that look and feel great on any platform with a comprehensive Flutter Course.
The quantifiers specify the number of occurrences of a character. Below table represents various quantifiers.
Regex | Description |
---|---|
X? | X occurs once or not at all |
X+ | X occurs once or more times |
X* | X occurs zero or more times |
X{n} | X occurs n times only |
X{n,} | X occurs n or more times |
X{y,z} | X occurs at least y times but less than z times |
Example:
import java.util.regex.*; public class Example{ public static void main(String args[]){ System.out.println("? quantifier ...."); //(a or y or z comes one time) System.out.println(Pattern.matches("[ayz]?", "a")); //output: true System.out.println(Pattern.matches("[ayz]?", "aaa")); //(a y and z comes more than one time) System.out.println(Pattern.matches("[ayz]?", "ayyyyzz")); //output: false //(a comes more than one time) System.out.println(Pattern.matches("[ayz]?", "amnta")); //output: false //(a or y or z must come one time) System.out.println(Pattern.matches("[ayz]?", "ay")); //output: false System.out.println("+ quantifier ...."); //(a or y or z once or more times) System.out.println(Pattern.matches("[ayz]+", "a")); //output: true //(a comes more than one time) System.out.println(Pattern.matches("[ayz]+", "aaa")); //outpu: true //(a or y or z comes more than once) System.out.println(Pattern.matches([amn]+", "aayyyzz")); //output: true //(z and t are not matching pattern) System.out.println(Pattern.matches("[ayz]+", "aammta")); //output: false System.out.println("* quantifier ...."); //(a or y or z may come zero or more times) System.out.println(Pattern.matches("[ayz]*", "ayyyza")); //output: true } }
Basically, it will search for the matching quantifier and matches the search result.
The regular expression metacharacters work as shortcodes. Let’s have a look at the below table to understand various types of metacharacters.
Regex | Description |
---|---|
. | It can be any character (may or may not match terminator) |
d | Represents any digits, short of [0-9] |
D | Represents any non-digit, short for [^0-9] |
s | Represents any whitespace character, short for [tnx0Bfr] |
S | It can be a non-whitespace character, short for [^s] |
w | It can be a word character, short for [a-zA-Z_0-9] |
W | Represents any non-word character, short for [^w] |
b | Represents a word boundary |
B | It is a non-word boundary |
Example:
import java.util.regex.*; public class MetacharExample{ public static void main(String args[]){ // d means digit System.out.println("metacharacters d...."); //(non-digit) System.out.println(Pattern.matches("d", "abc"));//Output: false //(digit and comes once) System.out.println(Pattern.matches("d", "1"));//Output: true //(digit but comes more than once) System.out.println(Pattern.matches("d", "4443")); //Output: false //(digit and char) System.out.println(Pattern.matches("d", "323abc"));//Output: false //D means non-digit System.out.println("metacharacters D...."); //(non-digit but comes more than once) System.out.println(Pattern.matches("D", "abc")); // Output: false //Its a Digit System.out.println(Pattern.matches("D", "1")); //Output: false System.out.println(Pattern.matches("D", "4443")); //Output: false // (digit and char) System.out.println(Pattern.matches("D", "323abc")); //Output: false //(non-digit and comes once) System.out.println(Pattern.matches("D", "m")); //Output: true System.out.println("metacharacters D with quantifier...."); //(non-digit and may come 0 or more times) System.out.println(Pattern.matches("D*", "abc")); //Output: true } }
Based on the above-mentioned conditions, it will display the output. That’s how it works. So, That was all about various types of Java Regex. With this, we come to the end of this article. I hope you found it informative. If you wish to learn more, you can check out our other Java Blogs as well.
Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.
Got a question for us? Please mention it in the comments section of this “ Java Regex” article and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 21st December,2024 21st December SAT&SUN (Weekend Batch) | View Details |
Java Course Online | Class Starts on 1st March,2025 1st March SAT&SUN (Weekend Batch) | View Details |
edureka.co