Share via


Wildcard search on List of string

Question

Wednesday, April 13, 2016 3:36 PM

I have a list of string and want to do wildcard search.

      List<string> list = new List<string>();
            list.Add("Hi how are you.");
            list.Add("are you there.");
            list.Add("How is it?.");
            list.Add("are you OK?");

in above list, i like to search like %are%you% which should return me 3 result. If i use Oracle then like query words well.

your assistance in this regard is deeply appreciated.

All replies (6)

Wednesday, April 13, 2016 5:25 PM ✅Answered

Mdew : Is this what you are looking for ?

  List<string> list = new List<string>();
            list.Add("Hi how are you.");
            list.Add("are you there.");
            list.Add("How is it?.");
            list.Add("are you OK?");

            List<string> result = list.Where(x => x.Contains("are you")).ToList();
            var count = result.Count;
            Console.WriteLine(count);
            if (count > 0)
            {

                foreach (var item in result) Console.WriteLine("{0}", item);

            }

Thursday, April 14, 2016 6:47 AM ✅Answered

Try this:

    List<string> result = list.Where( s => Regex.IsMatch( s, "are.*?you" ) ).ToList();

Similar to “%”, it will also detect “are” and “you” which are parts of other words. (Example: “Dare to be yourself”). The Regular Expression can be improved if you need non-Oracle search.


Thursday, April 14, 2016 6:06 AM

Thanks Magnus for your reply. But its not my objective. I like to search "%are%you%" same that we do in Oracle.


Thursday, April 14, 2016 6:58 AM

Thanks Viorel. It works


Thursday, April 14, 2016 7:02 AM

this search result 3 output out of 4. 

'are you' is not the first word in first list. Can we do anything to block it in search list? if i put % at initial then it should come.


Thursday, April 14, 2016 7:54 AM

'are you' is not the first word in first list. Can we do anything to block it in search list? if i put % at initial then it should come.

@mdew,

If you don't want "Hi how are you.", please try the following code,

  List<string> result = list.Where(s => Regex.IsMatch(s, "^are you*")).ToList();

//get results

are you there.

are you OK?

Best regards,

Kristin

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.