Codementor Events

Java Regular Expression: part 3 - Matching text for validation 2

Published Jan 11, 2019Last updated Apr 05, 2019
Java Regular Expression: part 3 - Matching text for validation 2

In the last part, we have used some simple patterns for the purposes of input validation. In this part, we are going to apply some more patterns that are a bit complex, including:

  • Checking a number in a certain range, not necessarily from 0 to 9.
  • Checking a character in a certain range, not necessarily from a to z.
  • Checking both numbers and characters in pre-defined ranges
  • Checking a string containing pre-defined characters
  • Checking a string not containg pre-defined characters

Case 1: Checking a number in a certain range

Suppose we are asking users to input their home address number, which must be: at least one digit, maximum of 3 digits, and digits must be from 5 to 9. That means digits 0 from 4 are not allowed.
With those requirements, I have 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 addressPattern = "[5-9]{1,3}";
            System.out.print("Input your house number: ");
            String input = sc.next();
            flag = input.matches(addressPattern);
            if (!flag) System.out.println("Invalid data!");
        } while (!flag);
        System.out.println("Valid data");
    }
}

In the above code, the most important part is the pattern:

String addressPattern = "[5-9]{1,3}";

In this pattern, I have used square brackets with 5-9 inside. That means the matched digits must be from 5 to 9, not necessarily in any particular order. Right after that is the restriction for min and max number of digits {1,3} which you should be familiar with by now.
Let’s run the program with some sample inputs:

Input your house number: a
Invalid data!
Input your house number: 156
Invalid data!
Input your house number: 78
Valid data

a: obviously not valid because it was not a digit
156: not valid because the digit 1 is not in range from 5 to 9
78: completely valid because both digits 7 and 8 are in range from 5 to 9, and also in allowed limit

Case 2: Checking a character in a certain range

Similar to the previous case, we can also require users to input a character that must lie in a certain range.
For instance, we ask users to input their home address street name which should only have:

  • Upper case letters A – E including: A, B, C, D, E
  • Upper case letters G – N including: G, H, I, J, K, L, M, N

Take a look at 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 addressPattern = "[A-EG-N]{2,8}";
            System.out.print("Input your street name: ");
            String input = sc.next();
            flag = input.matches(addressPattern);
            if (!flag) System.out.println("Invalid data!");
        } while (!flag);
        System.out.println("Valid data");
    }
}

In the above code, I have defined the pattern:

String addressPattern = "[A-EG-N]{2,8}";

Similar to the previous case, I have used the square brackets ([]) to define the desired ranges of characters. However, in this case, we need to specify 2 character ranges:

  • The first range is from A to E, and must be upper case
  • The second range is from G to N, and must be upper case

Note that there is no white space in between ranging characters. And it is also not necessary to follow any particular order as long as the inputted characters are in ranges from A to E or from G to N.
And I’m sure you know the meaning of {2,8}.
Run the program and we will have the following results:

Input your street name: AEF
Invalid data!
Input your street name: OP
Invalid data!
Input your street name: gek
Invalid data!
Input your street name: GEK
Valid data

AEF: invalid because F is not in the specified ranges
OP: invalid for the same reason
gek: invalid because although all characters are in the specified ranges, they were all lower case letters
GEK: completely matched the pattern

Case 3: Checking both numbers and characters in pre-defined ranges

There is no reason to stop us to combine both chateracters and digits into required ranges.
The following code should be explained for itself:

import java.util.Scanner;
public class Demo {
    public static void main(String[] args) {
        boolean flag;
        Scanner sc = new Scanner(System.in);
        do {
            String addressPattern = "[1-5A-EG-N]{2,8}";
            System.out.print("Input your street name: ");
            String input = sc.next();
            flag = input.matches(addressPattern);
            if (!flag) System.out.println("Invalid data!");
        } while (!flag);
        System.out.println("Valid data");
    }
}

Again, the order in the square brackets is not important.
If you find difficult to understand, here it is:

  • input digits must be from 1 to 5
  • input characters must be upper case and from A to E or G to N
  • input characters must be at leat 2 and maximum of 8 characters
  • no while space allowed

Run the program:

Input your street name: 8ACD
Invalid data!
Input your street name: Gc123
Invalid data!
Input your street name: GC123
Valid data

8ACD: invalid because the input contained the digit which is not in range from1 to 5
Gc123: invalid because the letter c is not upper case
GC123: completely matched the pattern

Case 4: Checking a string containing pre-defined single characters

Besides being restricted using ranges, we can also use range characters ([]) to impose on pre-defined single characters
Let’s see the following example:

import java.util.Scanner;
public class Demo {
    public static void main(String[] args) {
        boolean flag;
        Scanner sc = new Scanner(System.in);
        do {
            String addressPattern = "[1357ASDF]+";
            System.out.print("Input your street name: ");
            String input = sc.next();
            flag = input.matches(addressPattern);
            if (!flag) System.out.println("Invalid data!");
        } while (!flag);
        System.out.println("Valid data");
    }
}

The pattern:

String addressPattern = "[1357ASDF]+";

has the meaning:

  • the input string must contain one of the followings: 1, 3, 5, 6 and upper case letters: A, S, D, F
  • at least 1 character must be supplied since we have used the plus (+) sign

Run the program and we got:

Input your street name: 12ASD
Invalid data!
Input your street name: 13ACB
Invalid data!
Input your street name: AD57
Valid data

12ASD: invalid because the digit 2 was not one of the pre-defined digits
13ACB: invalid because letter C and B were not ones of the pre-defined characters
AD57: completely matched the pattern
Case 5: Checking a string not containg pre-defined characters
Instead of checking a string containing characters in pre-defined ranges, we can do the opposite, which means a string must not contain certain characters.
Let’s see an example:

import java.util.Scanner;
public class Demo {
    public static void main(String[] args) {
        boolean flag;
        Scanner sc = new Scanner(System.in);
        do {
            String addressPattern = "[^1357ASDF]+";
            System.out.print("Input your street name: ");
            String input = sc.next();
            flag = input.matches(addressPattern);
            if (!flag) System.out.println("Invalid data!");
        } while (!flag);
        System.out.println("Valid data");
    }
}

Take a closer look at the pattern:

String addressPattern = "[^1357ASDF]+";

The pattern looks similarly to the one in the previous example with one significant different: the caret (^) character. The caret (^) sign means ‘except’. So, the pattern will match strings containing any characters except one of the characters 1357ASDF
Run the program:

Input your street name: 135ASDF
Invalid data!
Input your street name: QWE
Valid data

135ASDF: invalid because all the characters in the string were not allowed
QWE: completely valid because there was no prohibited characters

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