Search code examples
c#regexstringcomparisonstring-comparison

Comparing String Captured in Application to Regex pattern in code with two different expected languages


I am working on an application that has several language options. The problem I am encountering is coming up with an efficient way to verify the text on the screen.

The text will either be 1 of 10 or 1 de 10 depending on the language

I was attempting to do something along the lines of

string availableDesignCountText = designSelectionPage.designSelectionAvailableDesignCountText.Text.ToLower().Trim();
System.Text.RegularExpressions.Regex.IsMatch(availableDesignCountText, @"^[\d]\s(of|de)\s[\d]");

availableDesignCountText would be the value captured from the application (1 of 10 or 1 de 10).

I would ultimately like to do an assertEquals that would compare the string to the regex pattern and would work for either language. Maybe something like

TestReporter.assertEquals(availableDesignCountText, expectedText, //regex maybe?
                "The Design Selection Page Available Design Text [" + closeButtonText + "] " +
                "Didn't Match the Expected Text [" + expectedText + "]");

Any Recommendations?

Thank you!


Solution

  •             // Available Design Count Text
                addTestStep("The Design Selection Available Design Text Matches the Expected Text");
                string availableDesignCountText = designSelectionPage.designSelectionAvailableDesignCountText.Text.ToLower().Trim();
                bool textMatches = System.Text.RegularExpressions.Regex.IsMatch(availableDesignCountText, @"^\d+\s+(?:of|de)\s+\d+$");
                TestReporter.assertTrue(textMatches,
                    "The Design Selection Page Available Design Text Didn't Match the Expected Format [" + availableDesignCountText + "]");
                addTestStep("Complete");
    

    This ended up doing the trick!