jeudi 11 juin 2020

How can I use Regex to test for these four specific characters? (+ - * /)?

I have the code to test for a string below, but I am having a difficult time finding documentation for how I would test for these four specific symbols (+ - * /) in my calculator app. Any suggestions are appreciated.

  if (!Regex.IsMatch(enteredOperation, "^[a-zA-Z]*$"))
                {
                    Console.WriteLine("Please restart the program and enter one of the symbols + - * / for the operation you want to complete");
                    break;
                }

I also tried the below method but other characters and numbers are getting past.

 if (!Regex.IsMatch(enteredOperation, "^(?=.*\\+).{1}$ || ^(?=.*\\-).{1}$ || ^(?=.*\\*).{1}$ || ^(?=.*\\/).{1}$")) 
                {
                    Console.WriteLine("Please restart the program and enter one of the symbols + - * / for the operation you want to complete");
                    break;
                }

Aucun commentaire:

Enregistrer un commentaire