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

Java正则表达式需求:匹配仅含4个连字符与13位数字的行

Correct Java Regex for Exact 4 Hyphens and 13 Digits (Any Order)

To match lines that contain exactly 4 hyphens (-) and exactly 13 digits (0-9) with no other characters (in any order), you can use a regex that combines positive lookaheads to enforce exact character counts, plus anchors to ensure we match the entire line.

The Regex Pattern

String regex = "^(?=(?:[^-]*-){4}[^-]*$)(?=(?:\\D*\\d){13}\\D*$)[\\d-]+$";

Breakdown of the Pattern

Let’s break down each component to understand how it enforces your requirements:

  1. Anchors (^ and $): Ensure we match the entire string/line, not just a substring.
  2. **First Lookahead ((?=(?:[^-]*-){4}[^-]*$)):
    • (?:[^-]*-): A non-capturing group that matches any number of non-hyphen characters followed by one hyphen.
    • Repeating this group 4 times ({4}) guarantees exactly 4 hyphens are present in the string.
    • [^-]*$: Matches any remaining non-hyphen characters until the end of the string.
  3. **Second Lookahead ((?=(?:\\D*\\d){13}\\D*$)):
    • (?:\\D*\\d): A non-capturing group that matches any number of non-digit characters followed by one digit.
    • Repeating this 13 times ({13}) guarantees exactly 13 digits are present.
    • \\D*$: Matches any remaining non-digit characters until the end.
  4. Main Match ([\\d-]+): Ensures the string only contains digits and hyphens (no other characters are allowed).

Example Usage in Java

Here’s how you can implement this regex to validate strings:

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

public class RegexValidator {
    public static void main(String[] args) {
        String valid1 = "123-345-565-45-67"; // 13 digits, 4 hyphens
        String valid2 = "----1234567890123"; // 13 digits, 4 hyphens
        String invalid1 = "123-1-34-5435-45-"; // 5 hyphens (too many)
        String invalid2 = "1234567890123"; // No hyphens (too few)
        String invalid3 = "abc-123-456-789-0123"; // Contains letters (invalid characters)

        String regex = "^(?=(?:[^-]*-){4}[^-]*$)(?=(?:\\D*\\d){13}\\D*$)[\\d-]+$";
        Pattern pattern = Pattern.compile(regex);

        validate(pattern, valid1);
        validate(pattern, valid2);
        validate(pattern, invalid1);
        validate(pattern, invalid2);
        validate(pattern, invalid3);
    }

    private static void validate(Pattern pattern, String input) {
        Matcher matcher = pattern.matcher(input);
        System.out.printf("Input '%s' is %s%n", input, matcher.matches() ? "VALID" : "INVALID");
    }
}

Output of the Example

Input '123-345-565-45-67' is VALID
Input '----1234567890123' is VALID
Input '123-1-34-5435-45-' is INVALID
Input '1234567890123' is INVALID
Input 'abc-123-456-789-0123' is INVALID

This regex will correctly reject any string that doesn’t meet your exact requirements—whether it has the wrong number of hyphens/digits, or contains non-digit/hyphen characters.

内容的提问来源于stack exchange,提问作者Dron4K

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:33:46