📘 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
Java has built-in API for working with regular expressions; it is located in java.util.regex package.
Java Regex tutorial at https://www.javaguides.net/2019/12/java-regex-tutorial-regular-expressions.html.
1. 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
2. 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
3. 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
4. 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
5. 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
6. 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 : ¥
7. 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>
8. 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
9. 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
10. 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
Comments
Post a Comment
Leave Comment