im trying to validate a string of text that must be in the following format,
The number “1” followed by a semicolon followed by between 1 and three numbers only – it would look something like this.
1:1 (correct)
1:34 (correct)
1:847 (correct)
1:2322 (incorrect)
There can be no letters or anything else except numbers.
Does anyone know how i can make this with REGEX? and in C#
The following pattern would do that for you:
Sample code:
For more convenient access you should probably put the validation into a separate method:
Explanation of the pattern:
The
^and$characters makes the pattern match the full string, instead of finding valid strings embedded in a larger string. Without them the pattern would also match strings like"1:2322"and"the scale is 1:234, which is unusual".