- List item
I use following Regex to validate a string ^[a-zA-Z0-9-/]*
private static void ValidateActualValue(string value)
{
if (String.IsNullOrEmpty(value)) throw new ArgumentNullException("value");
if (Regex.IsMatch(value, (@"^[a-zA-Z0-9-/]*")))
{
throw new InvalidBarcodeException(value);
}
}
The following string should be allowed string correctBarcodeString = “1-234567890/A”;
However there’s still an exception thrown.
Allowed values should be:
- 1234234545689889097
- A-adf90923409/1234
- aaaaaaaAAA
- BC-9876655788
- BC-345/q3435/wqer
- ABC-/BCD
- etc.
Inside a character group the
-has to be at the beginning or at the end, otherwise it has to be escaped.So change it to
Edit:
I would also suggest an anchor at the end of the regex, otherwise it will also match as long as the first part is valid.
if you want to avoid matching the empty string then use
+instead of*. Or if you know a valid Min/Max Range for the length use{4,20}, if the minimum amount of characters is 4 and the maximum is 20.