使用正则表达式匹配包含数字字母和短划线的字符串

n4r*_*zul 2 c# regex .net-3.5

我需要匹配这个字符串,011Q-0SH3-936729但不是345376346asfsdfgsfsdf 它必须包含字符和数字和破折号

模式可以是011Q-0SH3-936729011Q-0SH3-936729-SDF3000-222-AAAA011Q-0SH3-936729-011Q-0SH3-936729-011Q-0SH3-936729-011Q-0SH3-936729我希望它能够匹配任何人.原因是我不知道格式是否已修复且我无法找到,所以我需要为具有任意数量的破折号的模式提出通用解决方案,并且模式会重复出现任意数量的倍.

对不起,这可能是一个愚蠢的问题,但我真的很喜欢正则表达式.

TIA

Tim*_*ker 5

foundMatch = Regex.IsMatch(subjectString, 
    @"^             # Start of the string
    (?=.*\p{L})     # Assert that there is at least one letter
    (?=.*\p{N})     # and at least one digit
    (?=.*-)         # and at least one dash.
    [\p{L}\p{N}-]*  # Match a string of letters, digits and dashes
    $               # until the end of the string.", 
    RegexOptions.IgnorePatternWhitespace);
Run Code Online (Sandbox Code Playgroud)

应该做你想做的事.如果用字母/数字表示"只有ASCII字母/数字"(也不是国际/ Unicode字母),那么请使用

foundMatch = Regex.IsMatch(subjectString, 
    @"^             # Start of the string
    (?=.*[A-Z])     # Assert that there is at least one letter
    (?=.*[0-9])     # and at least one digit
    (?=.*-)         # and at least one dash.
    [A-Z0-9-]*      # Match a string of letters, digits and dashes
    $               # until the end of the string.", 
    RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
Run Code Online (Sandbox Code Playgroud)