.NET regular expression for a relatively simple string format [closed]
开发者_StackOverflow社区
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this questionI'm trying to construct a regular expression for the following:
- 6 characters exactly
- Alphanumerical characters only
- Letters are all uppercase
- There are exactly 1, 3, 4 or 5 letters at the beginning, the rest must be numbers
How do I go about that?
Here's how I'd write it (using C# in free-spacing mode):
if (Regex.IsMatch(text, @"
# Match specific string with multiple requirements.
^ # Anchor to start of string.
(?=.{6}$) # 6 characters exactly.
(?: # There are exactly
[A-Z]{3,5} # 3, 4, 5 or
| [A-Z] # 1, uppercase letters
) # at the beginning,
[0-9]+ # the rest must be numbers.
$ # Anchor to end of string.",
RegexOptions.IgnorePatternWhitespace)) {
// Successful match
} else {
// Match attempt failed
}
Edit: Changed from PHP to C#/.NET syntax.
I'd prefer to use more than just regex to get more humaa readability
var input = "ABC456";
return input.Length==6 && Regex.IsMatch(input,@"^([A-Z]|[A-Z]{3,5})\d+$");
Abdicate
Definition refactor:
- 6 total characters, capital letter and number chars only
- first must be a letter, last a number
- first letter is optionally followed by 2 to 4 letters
Method 1:
^(?=.{6}$)[A-Z](?:[A-Z]{2,4})?\d+$
(expanded)
^
(?= .{6} $ )
[A-Z] (?:[A-Z]{2,4})? \d+ $
Method 2:
^(?=[A-Z](?:(?<!\d)[A-Z]|\d){4}\d$)[A-Z](?:[A-Z]{2,4})?\d
(expanded)
^
(?= [A-Z] (?: (?<!\d)[A-Z] | \d ){4} \d $ )
[A-Z] (?:[A-Z]{2,4})? \d
Method 2 doesn't require an end anchor because the assertion is length and end specific, but is made to appear slow/cumbersome by the lookbehind (due to OP's conditions).
For this reason I would go with a Method 1 regex, although I believe the other would obtain better speed bench's, which is probably irrelavent over code clarity in this case.
^[A-Z](([A-Z]{2}[0-9]{3})|([A-Z]{3}[0-9]{2})|([A-Z]{4}[0-9]{1})|([0-9]{5}))$
UPDATED: Now matches all requirements
^([A-Z]\d{5})|([A-Z]{3}\d{3})|([A-Z]{4}\d{2})|([A-Z]{5}\d)$
This should do the trick on the top of my head. There may be shorter more elegant versions..
bool foundMatch = false;
try {
foundMatch = Regex.IsMatch(subjectString, @"\A(?:(?:[A-Z]\d{5})|(?:[A-Z]{3}\d{3})|(?:[A-Z]{4}\d{2})|(?:[A-Z]{5}\d))\Z");
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
精彩评论