1

I am trying to make a while loop to ensure employee is in correct format - however it works only when I put the employee ID correctly the first time.

When I put first a wrong format to the ID and then put a correct one it does not revaluate and identify it as true

Here is the part of the code in question:

Console.Write("Please enter your employee ID:");
empID = Console.ReadLine();

string pattern = @"^\d{9}[A-Z]{1}$";

Match match = Regex.Match(empID, pattern);

while (match.Success != true)
{

    if (match.Success == true)
    {
        Console.WriteLine(empID);
    }
    else
    {
        Console.WriteLine("Incorrect employee ID - please try again");
        empID = Console.ReadLine();

    }
}

Any idea what it could be that it does not see empID as correct when entered correctly second time?

Thanks

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 2
    Are you expecting `match.Success` to be reevaluated when `empID` changes? It won't be. – Lance U. Matthews Dec 02 '19 at 03:07
  • Does this answer your question? [Setting a relevant expression match to false in a do...while](https://stackoverflow.com/questions/24690630/setting-a-relevant-expression-match-to-false-in-a-do-while) – Lance U. Matthews Dec 02 '19 at 03:09
  • Aside: `match.Success == true` ought to have a value very close to `match.Success`. Similarly, `while ( !match.Success )` ought to be sufficient unless you're checking for `true` changing value. – HABO Dec 02 '19 at 03:14
  • I had problems with Regex matching when copying the input from a webpage and the input string included "invisible" characters like line breaks (\n) and other whitespace characters (https://en.wikipedia.org/wiki/Whitespace_character). If that's the case you need to trim the input string before Regex matching it > https://stackoverflow.com/questions/6219454/efficient-way-to-remove-all-whitespace-from-string – Joel Wiklund Dec 02 '19 at 07:08
  • Hi - thanks for your replies. @BACON - yes I am trying to revaluate the false until someone enter a correct value. But it ignores the fact someone eventually entered a valid empID. when I try your suggested approach it tells me my "match" is not yet defined therefore not working. HABO- I am trying to get a loop that looks at empID until it is a valid one. Then it continues in the code. I thought with Regex I could do that but seems not? – user12466101 Dec 02 '19 at 10:12
  • @user12466101 As others have suggested you need to apply the regex to each input inside the (`do`/`while`) loop. Testing only the first input and then getting additional input won't work. I was suggesting that although you _can_ write `if (match.Success == true)` or `if ((match.Success( == ((true)==((true))))` or other curious variations, the end result is simply `if (match.Success)`. Similarly `while (match.Success != true)` is `while (!match.Success)` although more flowery versions are certainly possible. – HABO Dec 02 '19 at 13:20
  • I realized there was some ambiguity in my first comment. Of course `match.Success` will be reevaluated because it is part of the condition of the `while` loop. `match.Success` will not be reevaluated internally by that `Match` instance, though, because it is based on the input text passed to the call to `Match()`; changing `empID` to a new string doesn't change the string referenced by and against which the `Match` was evaluated. As the answer and suggested duplicate question show, if you want to evaluate a regex against a different input string, you need to create a new `Match` instance. – Lance U. Matthews Dec 02 '19 at 19:12

1 Answers1

8

You don't update match variable value in the loop. Please see my comments in the code

// here you receive your employee id
Console.Write("Please enter your employee ID:"); empID = Console.ReadLine();

string pattern = @"^\d{9}[A-Z]{1}$";

// here you initialize your match variable by result of matching with regex
Match match = Regex.Match(empID, pattern);

while (match.Success != true)
{
    if (match.Success == true)
    {
        Console.WriteLine(empID);
    }
    else
    {
        Console.WriteLine("Incorrect employee ID - please try again");

        // here you read your new employee id, but match variable is not updated
        empID = Console.ReadLine();

        // this is what you missed
        match = Regex.Match(empID, pattern);
    }
}
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
Michael Kokorin
  • 474
  • 2
  • 8