📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Table of Contents
- Regular Expressions
- java.util.regex package
- Character classes
- Predefined character classes
- Java Simple Regular Expression
- Java Alphanumeric Regex Example
- Java Regex Anchors
- Java Regex alternations
- Regular Expression Phone Number validation
- Java Regex for Matching any Currency Symbol Example
- Java Regex capturing groups
- Java case-insensitive regular expression
- Java Regex email example
- Java Regex to check Min/Max Length of Input Text
1. Regular Expressions
Java has built-in API for working with regular expressions; it is located in java.util.regex package.
Regex | Meaning |
---|---|
. | Matches any single character. |
? | Matches the preceding element once or not at all. |
+ | Matches the preceding element once or more times. |
* | Matches the preceding element zero or more times. |
^ | Matches the starting position within the string. |
$ | Matches the ending position within the string. |
| | Alternation operator. |
[abc] | Matches a or b, or c. |
[a-c] | Range; matches a or b, or c. |
[^abc] | Negation matches everything except a, or b, or c. |
\s | Matches white space character. |
\w | Matches a word character; equivalent to [a-zA-Z_0-9] |
2. java.util.regex package
- MatchResult interface
- Matcher class
- Pattern class
- PatternSyntaxException class
Matcher is an engine that interprets the pattern and performs match operations against an input string. Matcher has methods such as find(), matches(), end() to perform matching operations. When there is an exception parsing a regular expression, Java throws a PatternSyntaxException.
3. Character Classes
Construct | 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) |
4. Predefined Character Classes
Construct | Description |
---|---|
. | Any character (may or may not match line terminators) |
\d | A digit: [0-9] |
\D | A non-digit: [^0-9] |
\s | A whitespace character: [ \t\n\x0B\f\r] |
\S | A non-whitespace character: [^\s] |
\w | A word character: [a-zA-Z_0-9] |
\W | A non-word character: [^\w] |
5. Java Simple Regular Expression
package net.javaguides.corejava.regex;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaRegexExample {
public static void main(String[] args) {
List < String > words = Arrays.asList("One", "Two",
"Three", "Four", "Five", "Six", "Seven", "Maven", "Amen", "eleven");
Pattern p = Pattern.compile(".even");
for (String word: words) {
Matcher m = p.matcher(word);
if (m.matches()) {
System.out.printf("%s -> matches%n", word);
} else {
System.out.printf("%s -> does not match%n", word);
}
}
}
}
One -> does not match
Two -> does not match
Three -> does not match
Four -> does not match
Five -> does not match
Six -> does not match
Seven -> matches
Maven -> does not match
Amen -> does not match
eleven -> does not match
6. Java Alphanumeric Regex Example
package net.javaguides.corejava.regex;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaAlphanumericRegex {
public static void main(String[] args) {
List < String > names = new ArrayList < String > ();
names.add("JavaGuides");
names.add("JavaGuides123");
names.add("JavaGuides-----////"); //Incorrect
String regex = "^[a-zA-Z0-9]+$";
Pattern pattern = Pattern.compile(regex);
for (String name: names) {
Matcher matcher = pattern.matcher(name);
System.out.println(matcher.matches());
}
}
}
true
true
false
7. Java Regex Anchors
package net.javaguides.corejava.regex;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaRegexAnchorExample {
public static void main(String[] args) {
List < String > sentences = Arrays.asList("I am looking for Prabhas",
"Prabhas is an Actor",
"Mahesh and Prabhas are close friends");
Pattern p = Pattern.compile("^Prabhas");
for (String word: sentences) {
Matcher m = p.matcher(word);
if (m.find()) {
System.out.printf("%s -> matches%n", word);
} else {
System.out.printf("%s -> does not match%n", word);
}
}
}
}
I am looking for Prabhas -> does not match
Prabhas is an Actor -> matches
Mahesh and Prabhas are close friends -> does not match
8. Java Regex alternations
package net.javaguides.corejava.regex;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaRegexAlternation {
public static void main(String[] args) {
List < String > users = Arrays.asList("Ramesh", "Tom", "Tony",
"Rocky", "John", "Prabhas");
Pattern p = Pattern.compile("Ramesh|Tom|Prabhas|Rocky");
for (String user: users) {
Matcher m = p.matcher(user);
if (m.matches()) {
System.out.printf("%s -> matches%n", user);
} else {
System.out.printf("%s -> does not match%n", user);
}
}
}
}
Ramesh -> matches
Tom -> matches
Tony -> does not match
Rocky -> matches
John -> does not match
Prabhas -> matches
9. Regular Expression Phone Number validation
package net.javaguides.corejava.regex;
public class CheckPhoneExample {
public static void main(String[] args) {
System.out.println("Phone number 1234567890 validation result: " + validatePhoneNumber("1234567890"));
System.out.println("Phone number 123-456-7890 validation result: " + validatePhoneNumber("123-456-7890"));
System.out.println(
"Phone number 123-456-7890 x1234 validation result: " + validatePhoneNumber("123-456-7890 x1234"));
System.out.println(
"Phone number 123-456-7890 ext1234 validation result: " + validatePhoneNumber("123-456-7890 ext1234"));
System.out.println("Phone number (123)-456-7890 validation result: " + validatePhoneNumber("(123)-456-7890"));
System.out.println("Phone number 123.456.7890 validation result: " + validatePhoneNumber("123.456.7890"));
System.out.println("Phone number 123 456 7890 validation result: " + validatePhoneNumber("123 456 7890"));
}
private static boolean validatePhoneNumber(String phoneNo) {
// validate phone numbers of format "1234567890"
if (phoneNo.matches("\\d{10}"))
return true;
// validating phone number with -, . or spaces
else if (phoneNo.matches("\\d{3}[-\\.\\s]\\d{3}[-\\.\\s]\\d{4}"))
return true;
// validating phone number with extension length from 3 to 5
else if (phoneNo.matches("\\d{3}-\\d{3}-\\d{4}\\s(x|(ext))\\d{3,5}"))
return true;
// validating phone number where area code is in braces ()
else if (phoneNo.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}"))
return true;
// return false if nothing matches the input
else
return false;
}
}
Phone number 1234567890 validation result: true
Phone number 123-456-7890 validation result: true
Phone number 123-456-7890 x1234 validation result: true
Phone number 123-456-7890 ext1234 validation result: true
Phone number (123)-456-7890 validation result: true
Phone number 123.456.7890 validation result: true
Phone number 123 456 7890 validation result: true
10. Java Regex for Matching any Currency Symbol Example
package net.javaguides.corejava.regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaRegexCurrencySymbol {
public static void main(String[] args) {
String content = "Let's find the symbols or currencies: $ Dollar, € Euro, ¥ Yen";
String regex = "\\p{Sc}";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.print("Start index: " + matcher.start());
System.out.print(" End index: " + matcher.end() + " ");
System.out.println(" : " + matcher.group());
}
}
}
Start index: 39 End index: 40 : $
Start index: 49 End index: 50 : €
Start index: 57 End index: 58 : ¥
11. Java Regex capturing groups
package net.javaguides.corejava.regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaRegexGroups {
public static void main(String[] args) {
String content = "<p>The <code>Pattern</code> is a compiled " +
"representation of a regular expression.</p>";
Pattern p = Pattern.compile("(</?[a-z]*>)");
Matcher matcher = p.matcher(content);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}
}
<p>
<code>
</code>
</p>
12. Java case-insensitive regular expression
package net.javaguides.corejava.regex;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaRegexCaseInsensitive {
public static void main(String[] args) {
List < String > users = Arrays.asList("dog", "Dog", "DOG", "Doggy");
Pattern p = Pattern.compile("dog", Pattern.CASE_INSENSITIVE);
users.forEach((user) - > {
Matcher m = p.matcher(user);
if (m.matches()) {
System.out.printf("%s matches%n", user);
} else {
System.out.printf("%s does not match%n", user);
}
});
}
}
dog matches
Dog matches
DOG matches
Doggy does not match
13. Java Regex email example
package net.javaguides.corejava.regex;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaRegexEmail {
public static void main(String[] args) {
List < String > emails = Arrays.asList("ramesh@gmail.com",
"tom@yahoocom", "34234sdfa#2345", "tony@gmail.com");
String regex = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\\.[a-zA-Z.]{2,18}$";
Pattern p = Pattern.compile(regex);
for (String email: emails) {
Matcher m = p.matcher(email);
if (m.matches()) {
System.out.printf("%s matches%n", email);
} else {
System.out.printf("%s does not match%n", email);
}
}
}
}
ramesh@gmail.com matches
tom@yahoocom does not match
34234sdfa#2345 does not match
tony@gmail.com matches
14. Java Regex to check Min/Max Length of Input Text
package net.javaguides.corejava.regex;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMinMaxLength {
public static void main(String[] args) {
List < String > names = new ArrayList < String > ();
names.add("RAMESH");
names.add("JAVAGUIDES");
names.add("RAMESHJAVAGUIDES"); //Incorrect
names.add("RAMESH890"); //Incorrect
String regex = "^[A-Z]{1,10}$";
Pattern pattern = Pattern.compile(regex);
for (String name: names) {
Matcher matcher = pattern.matcher(name);
System.out.println(matcher.matches());
}
}
}
true
true
false
false
nice tutorial of regex.
ReplyDelete