我需要匹配这个字符串,011Q-0SH3-936729但不是345376346或asfsdfgsfsdf
它必须包含字符和数字和破折号
模式可以是011Q-0SH3-936729或011Q-0SH3-936729-SDF3或000-222-AAAA或011Q-0SH3-936729-011Q-0SH3-936729-011Q-0SH3-936729-011Q-0SH3-936729我希望它能够匹配任何人.原因是我不知道格式是否已修复且我无法找到,所以我需要为具有任意数量的破折号的模式提出通用解决方案,并且模式会重复出现任意数量的倍.
对不起,这可能是一个愚蠢的问题,但我真的很喜欢正则表达式.
TIA
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)