How Tos

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

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.

Categories: How Tos, Java

Tagged as:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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