You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求生成验证美国SIT ID的Java正则表达式(53/54/55开头后接7位数字)

Validating SIT IDs in Java (Starts with 53/54/55, Followed by 7 Digits)

Got it, let's break down how to build the perfect regex for your SIT ID validation requirement. First, here's the ready-to-use pattern you can drop into your code:

String sitIdRegex = "^(53|54|55)\\d{7}$";

Let's unpack what each part does:

  • ^ : Makes sure the match starts right at the beginning of the string (no extra characters before the ID)
  • (53|54|55) : A grouping that matches exactly one of the three allowed starting pairs: 53, 54, or 55
  • \\d{7} : Matches exactly 7 numeric digits (the double backslash is required in Java strings to escape the regex backslash)
  • $ : Ensures the match ends right at the end of the string (no extra characters after the ID)

Example Implementation

Here's a quick Java class to demonstrate how to use this regex for validation:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class SitIdValidator {
    // Compile the pattern once for better performance
    private static final Pattern SIT_ID_PATTERN = Pattern.compile("^(53|54|55)\\d{7}$");

    public static boolean isValidSitId(String sitId) {
        // Handle null input first
        if (sitId == null) {
            return false;
        }
        Matcher matcher = SIT_ID_PATTERN.matcher(sitId);
        // matches() checks the entire string against the pattern
        return matcher.matches();
    }

    public static void main(String[] args) {
        // Test some cases to verify
        System.out.println(isValidSitId("531234567")); // ✅ Valid
        System.out.println(isValidSitId("549876543")); // ✅ Valid
        System.out.println(isValidSitId("550000000")); // ✅ Valid
        System.out.println(isValidSitId("521234567")); // ❌ Invalid (starts with 52)
        System.out.println(isValidSitId("53123456"));  // ❌ Invalid (only 6 digits after start)
        System.out.println(isValidSitId("5312345678"));// ❌ Invalid (8 digits after start)
        System.out.println(isValidSitId("53abcdefg")); // ❌ Invalid (non-digit characters)
    }
}

Quick Notes:

  • Remember the double backslash (\\d) in Java! Regex uses \d for digits, but Java strings require escaping backslashes, so we use two.
  • While matches() checks the entire string (so you could technically omit ^ and $), including them makes the regex more explicit and works with other matching methods if you ever need them.

内容的提问来源于stack exchange,提问作者Guruprasad A N

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:22:25