Home » Java » Core Java » How to use regular expression in Java?

About Mohamed Sanaulla

Mohamed Sanaulla

How to use regular expression in Java?

Regular expressions are very important tool for seraching in text. Below is the code snippet for executing regex search and capturing different parts of the string based on the regular expression

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class RegexTest {
 
    public static void main(String[] args) {
 
        String name = "01_My-File.pdf";
        match(name);
        match("09_03_12File.docx");
        match("09_03_12File.q123");
 
 
    }
 
    public static void match(String input){
        System.out.println("********* Analysing " + input+" *********");
        String regex = "([0-9]+)([_])(.*)([\\.])([A-Za-z]+)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        if (!matcher.matches()){
            System.out.println("Input not matches regex");
            return;
        }
        System.out.println("Matches: " + matcher.matches());
        String number = matcher.group(1);
        System.out.println("Index: " + number);
        String fileName = matcher.group(3);
        System.out.println("FileName: " + fileName);
        String extension = matcher.group(5);
        System.out.println("Extension: " + extension);
    }
}

The groups are captured by using (). In the regular expression above ([0-9]+)([_])(.*)([\.])([A-Za-z]+)

  • the first group is defined as a number with at least 1 digit
  • the second group is the fixed character _
  • the third group is any text.
  • the fourth group is the fixed character. (we have to escape . using \\ because in the regular expression a . means any character or symbol or space or number).
  • the fifth group is a group of characters with length > 0.

We use the Pattern class to compile the regular expression and match the input using it to result in a Matcher instance. This Matcher has information about the result of the regular expression match.

Published on Java Code Geeks with permission by Mohamed Sanaulla, partner at our JCG program. See the original article here: How to use regular expression in Java?

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ElenaGillbert
4 months ago

Hi…
I’m Elena gillbert.Regular Expressions or Regex (in short) is an API for defining String patterns that can be used for searching, manipulating and editing a string in Java. Email validation and passwords are few areas of strings where Regex are widely used to define the constraints. Regular Expressions are provided under java.util.regex package. This consists of 3 classes and 1 interface.