I am validating input strings using regex.
The input string must be 8 characters long, it must begin with an 'E', followed by six digits then either another digit or 'X'.
My Powershell comparison is as follows:
$Input -match "E\d{6}[\dX]"
This returns true for valid values and false for invalid values, as long as they are 8 characters or less. Unfortunately it also returns true for values where the first 8 characters match the expression but additional unwanted characters are also present, e.g:
input | desired return | actual return |
---|---|---|
Invalid1 | False | False |
E012345X | True | True |
E012345Xblahblahblah | False | True |
No doubt this is by design, however I would like it to return false for the last example. I cannot simply truncate the input as the script is validating existing data and needs to highlight the error.
Is there a way to do what I want in regex, or do I just have to use a combination of regex then test the string length separately?