10 Java Regular Expression (Java Regex) Examples

In this post, we will look into 10 useful Java regular expression examples.

Regular expressions are used for text searching and more advanced text manipulation. 

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.

10 Useful Java Regular Expression Examples

1. Java Simple Regular Expression
2. Java Alphanumeric Regex Example
3. Java Regex Anchors
4. Java Regex alternations
5. Regular Expression Phone Number validation
6. Java Regex for Matching any Currency Symbol Example
7. Java Regex capturing groups
8. Java case-insensitive regular expression
9. Java Regex email example
10. Java Regex to check Min/Max Length of Input Text

1. Java Simple Regular Expression

In the example, we have ten words in a list. We check which words match the .even 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);
            }
        }
    }
}
Output:
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
We compile the pattern. The dot (.) metacharacter stands for any single character in the text.

2. Java Alphanumeric Regex Example

This Java example demonstrates how to write a regular expression to validate user input in such a way that it allows only alphanumeric characters. Alphanumeric characters are all alphabets and numbers i.e. letters A–Za–z, and digits 0–9.
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());
        }
    }
}
Output:
true
true
false

3. Java Regex Anchors

Anchors match the positions of characters inside a given text. In the next example, we look if a string is located at the beginning of a sentence.
In the below example, we have three sentences. The search pattern is ^Prabhas. The pattern checks if the "Prabhas" string is located at the beginning of the text. Prabhas.$ would look for "Prabhas" at the end of the sentence.
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);
            }
        }
    }
}
Output:
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

The alternation operator | enables to create a regular expression with several choices.
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);
            }
        }
    }
}
Output:
Ramesh -> matches
Tom -> matches
Tony -> does not match
Rocky -> matches
John -> does not match
Prabhas -> matches

5. Regular Expression Phone Number validation

Validating a phone number using regular expression is tricky because the phone number can be written in many formats and can have extensions also.
For example, here are some of the common way of writing phone numbers:
1234567890 
123-456-7890 
123-456-7890 x1234 
123-456-7890 ext1234 
(123)-456-7890 
123.456.7890 
123 456 7890
Here is the Java program to demonstrates how to use a regular expression to validate phone numbers:
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;

    }
}
Output:
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

This Java Regex example demonstrates how to match all available currency symbols, e.g. $ Dollar, € Euro, ¥ Yen, in some text content.
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());
        }
    }
}
Output:
Start index: 39 End index: 40  : $
Start index: 49 End index: 50  : €
Start index: 57 End index: 58  : ¥

7. Java Regex capturing groups

The capturing group's technique allows us to find out those parts of the string that match the regular pattern. The mather's group() method returns the input subsequence captured by the given group during the previous match operation.
This example prints all HTML tags from the supplied string by capturing a group of characters.
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));
        }
    }
}
Output:
<p>
<code>
</code>
</p>

8. Java case-insensitive regular expression

By setting the Pattern.CASE_INSENSITIVE flag, we can have case-insensitive matching.
The example performs a case-insensitive matching of the 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);
            }
        });
    }
}
Output:
dog matches
Dog matches
DOG matches
Doggy does not match

9. Java Regex email example

In the following example, we create a regex pattern for checking email addresses. This example provides only one possible solution.
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);
            }
        }
    }
}
Output:
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

The following regular expression ensures that text is between 1 and 10 characters long, and additionally limits the text to the uppercase letters A–Z. You can modify the regular expression to allow any minimum or maximum text length or allow characters other than A–Z.
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());
        }
    }
}
Output:
true
true
false
false

Comments