Search code examples
c#user-input

How do i check if something contains XY, but it can also be XZY?


Here is the code

    Console.WriteLine("Hello!");
    string greeting = Console.ReadLine();
    if (greeting.ToLower().Contains("hello how are you")) ;
    {
        Console.WriteLine("I am good");
    }

This code has a problem, if the user says something like Hello jfjf how fjjffj are you the program would still reply to him. What i need is greeting has to be Hello, then X amount of any string (where each string would be split by a space) , how are you, so something like Hello thomas anderson how are you would work but something like Hello 8grrgh how jrjr are me you would not work


Solution

  • I suppose you´ll need a regex for this:

    var r = new Regex("Hello .* how are you");
    if(r.IsMatch(myString)) 
    { 
        Console.WriteLine("I am good");
    }
    

    This will match "Hello thomas anderson how are you" but not "Hello jfjf how fjjffj are you".