Using the matches method of the String class

2021-06-05

Hello everyone. This page is an English translation of a Japanese page. (The original Japanese has been slightly rewritten to make it easier to translate into English.)

This time, I used the matches method of the String class. This method specifies a regular expression as an argument, and judges whether the pattern is satisfied or not. For example, in a situation where you are asked to enter a numerical value, you can use matches to determine if the string you entered is a numerical value or not.

Determine if it is an integer

For example, suppose you want to determine whether a string is an integer value or not.

We will come up with a regular expression that fits our purpose.

It allows numbers with more than one digit, such as “1" or “12". So we will use “\d". The regular expression “\d" represents a single character between 0 and 9. Next, we decide that “01" and “02" should not be considered as numbers. In this case, “[1-9]+\d*" seems to be a good choice. The “[1-9]" represents a single character from “1 to 9", and the “+" means that there is at least one preceding character. So, the first character must be non-zero.

Also, the symbol “*" means “the previous character is zero or more times". So numbers with two digits or more can be determined. However, since “0" is not included in the list, we will use “(0|[1-9]+\d*)". The regular expression “X|Y" represents “X or Y". Finally, we consider the sign. The regular expression including the sign is “[+-]? (0|[1-9]+\\d*)". The regular expression “X?" means “X is 1 or 0 times".

The following are the source and execution results.

Source
public class HelloMatchInt {

    private static final String REGEX = "[+-]?(0|[1-9]+\\d*)";

    public static void main(String[] args) {
        test("a");
        test("+");
        test("1");
        test("+1");
        test("-1");
        test("0");
        test("2");
        test("34");
        test("01");
        test("012");
    }

    private static void test(String targetStr) {
        if (targetStr.matches(REGEX)) {
            int num = Integer.parseInt(targetStr);
            System.out.println(targetStr + "is an integer value. -> " + num);
        } else {
            System.out.println(targetStr + "is not an integer value.");
        }
    }

}
Execution results
ais not an integer value.
+is not an integer value.
1is an integer value. -> 1
+1is an integer value. -> 1
-1is an integer value. -> -1
0is an integer value. -> 0
2is an integer value. -> 2
34is an integer value. -> 34
01is not an integer value.
012is not an integer value.

As described above, we can determine whether a string is an integer or not.

Determine if it is a decimal number

The following regular expression is good for decimals.

Source
    private static final String REGEX = "[+-]?(0|[1-9]+\\d*)(\\.\\d+)?";

In the regular expression above, we have added a decimal representation. For decimals, the period itself represents an arbitrary character, so it needs to be escaped to “\. since the period itself represents an arbitrary character. The regular expression “(\\. (\\d+)? will have 1 or 0 decimal places.

The following are the source and execution results.

Source
public class HelloMatchDouble {

    private static final String REGEX = "[+-]?(0|[1-9]+\\d*)(\\.\\d+)?";

    public static void main(String[] args) {

        test("a");
        test("+");
        test("1");
        test("+1");
        test("-1");
        test("2");
        test("34");
        test("01");
        test("012");

        test("0a");
        test("0.");
        test("0");
        test("0.1");
        test("12.34");

    }

    private static void test(String targetStr) {

        if (targetStr.matches(REGEX)) {
            double num = Double.parseDouble(targetStr);
            System.out.println(targetStr + "is a number. -> " + num);
        } else {
            System.out.println(targetStr + "is not a number.");
        }

    }

}
Execution results
ais not a number.
+is not a number.
1is a number. -> 1.0
+1is a number. -> 1.0
-1is a number. -> -1.0
2is a number. -> 2.0
34is a number. -> 34.0
01is not a number.
012is not a number.
0ais not a number.
0.is not a number.
0is a number. -> 0.0
0.1is a number. -> 0.1
12.34is a number. -> 12.34

The regular expressions listed here are just examples. Please note that they may be incomplete.

That’s all. I hope this is helpful to you.