Home > Software design >  Java: Regular expression to match ###-9999
Java: Regular expression to match ###-9999

Time:10-10

I am looking for a regular expression so I can verify an student ID with this format:

###-9999

Here, ### means 3 uppercase letters, followed by 4 numbers in the range of 1-9. So far I have made this expression:

[A-Z]{3})(-)(\\d{4}

However, it doesn't seem to work as intended.

CodePudding user response:

if your range of number excludes 0, you shouldn't use \d but rather a group of characters [1-9]

so your Regex would look like [A-Z]{3}-[1-9]{4}

CodePudding user response:

Notice that Regular Expressions can be costly to compute because it must be compiled each run. Thus utilizing a pre-compiled pattern helps reducing this cost.

Below is an example of a benchmark from this source:

Time for String: 67.693 ms
Time for Pattern: 12.178 ms

Here is a runnable approach for your Student ID scenario:

RegexTest.java

import java.util.List;
import java.util.regex.Pattern;

class Patterns {

    public static final String STUDENT_ID_REGEX = "[A-Z]{3}-[0-9]{4}";

    private static final Pattern STUDENT_ID_PATTERN = Pattern.compile(STUDENT_ID_REGEX);

    public static boolean isStudentId(String candidate) {
        return STUDENT_ID_PATTERN.matcher(candidate).matches();
    }
}

public class RegexTest {

    public static void main(String[] args) {

        List<String> studentIds = List.of("ABC-1234", "ABC_1234", "AB-1234", "ABC-123", "abc-1234", "aBc-1234", "ABCD-A123", "SomeString", "123-ABCD");

        studentIds.forEach(id -> {
            System.out.println(id   " matches "   Patterns.STUDENT_ID_REGEX   "? "   Patterns.isStudentId(id));
        });
    }
}

Output

ABC-1234 matches [A-Z]{3}-[0-9]{4}? true
ABC_1234 matches [A-Z]{3}-[0-9]{4}? false
AB-1234 matches [A-Z]{3}-[0-9]{4}? false
ABC-123 matches [A-Z]{3}-[0-9]{4}? false
abc-1234 matches [A-Z]{3}-[0-9]{4}? false
aBc-1234 matches [A-Z]{3}-[0-9]{4}? false
ABCD-A123 matches [A-Z]{3}-[0-9]{4}? false
SomeString matches [A-Z]{3}-[0-9]{4}? false
123-ABCD matches [A-Z]{3}-[0-9]{4}? false

Environment

AdoptOpenJDK jdk-16.0.1.9-hotspot
  • Related