Codementor Events

Java Regular Expression: part 5 - More on grouping techniques

Published Jan 18, 2019Last updated Apr 05, 2019
Java Regular Expression: part 5 - More on grouping techniques

In the last part, we used grouping technique to optionally allow users to provide inputs. In this part, you will see that grouping technique can be applied in other cases.
Another case of utilizing grouping technique is to provide a list of items so that users can select from.
Let’s see the following code:

import java.util.Scanner;
public class Demo {
    public static void main(String[] args) {
        boolean flag;
        Scanner sc = new Scanner(System.in);
        do {
            String favorPattern = "(sport|music|book|movie)";
            System.out.print("Input a favorite (sport,music,book,movie): ");
            String input = sc.next();
            flag = input.matches(favorPattern);
            if (!flag) System.out.println("Invalid data!");
        } while (!flag);
        System.out.println("Valid data");
    }
}

In the code, I have had the pattern:

String emailPattern = "(sport|music|book|movie)";

To provide a list of items so that users can choose from, we need to put those items in a group and separated each item by the vertical bar. That means users can only input one of those in the group.
Let’s run the program and see:

Input a favorite (sport,music,book,movie): reading
Invalid data!
Input a favorite (sport,music,book,movie): sport
Valid data

“reading”: invalid because it was not in the list
“sport”: valid because it was in the list

Another case is that we can apply grouping technique in checking date time formats which are very common in data inputs.
Let’s see the following code:

import java.util.Scanner;
public class Demo {
    public static void main(String[] args) {
        boolean flag;
        Scanner sc = new Scanner(System.in);
        do {
            String dobPattern = "([1-9]{1,2}/)?([1-9]{1,2})/([0-9]{4})";
            System.out.print("Input your day of birth: ");
            String input = sc.next();
            flag = input.matches(dobPattern);
            if (!flag) System.out.println("Invalid data!");
        } while (!flag);
        System.out.println("Valid data");
    }
}

In the code, I have used the following pattern to check birthday format:

String dobPattern = "([1-9]{1,2}/)?([1-9]{1,2})/([0-9]{4})";

In the pattern, I have 3 groups:
Group 1: ([1-9]{1,2}/)?
This is for the date. In this group, users must input at least 1 digit and max of 2 digits from 1 to 9; followed by / to separate with the month. Users can ignore this part.
Group 2: ([1-9]{1,2})
This is for the month. I’m sure you can understand what it means. Well, it is not different from the group 1.
Group 3: ([0-9]{4})
This is for the year. Users must input all 4 digits
Let’s run and check:

Input day of birth: 1212/1980
Invalid data!
Input day of birth: 12/1980
Valid data
Input day of birth: 1/12/1980
Valid data

1212/1980: invalid because there was no / between date and month
12/1980: completely valid because users can ignore the date
1/12/1980: also valid

Matching text for validation is only one of the applications of regular expression. There are countless ways where we can apply regular expressions.
Let’s proceed to the next part to see we can apply regular expressions for string manipulation

Previous part

Next part

--

Visit learnbyproject.net for a free Regular Expression courses and other free courses

Discover and read more posts from Sera.Ng
get started