1.11M Members

getting a matching string

 
0
 

hi,

I have to write an algorithm for getting a matching string from list of saves string
after comparing it with the given string.

say example:

Saved string:
Book ABC
Book ABC XYZ
Note Copy
Note Book YYYe`

If user gives Book, then the algorithm can return Book ABC or Book ABC XYZ or Note Book YYY.
if the user gives Book ABC, the the algorithm can return Book ABC or Note Book.
if the user gives Book ABC XYZ, the the algorithm should return only Book ABC XYZ.
and so on.

Hope I am able to comunicate.

Have anyone written anything like this earlier or is c# provides any such functionalities?

Regards,

 
0
 

Yes.
System.String has a method called Contains() where you can supply the substring and it will return a bool telling you if the original source string Contains the substring.

 
0
 

Sorry, let me describe the question again..

Say the saved strings basically patterns are:

Book*
Book Two states
Shedney sheldon
sydney Book
Shopoholic*
Shopoholic part II
Shopoholic part III

Actually, we will be storing few parameters related to this patterns which will be returned to the user if any input strings matches any of the saved pattern. The saved strings will be in a collection.

eg.,

  1. if the input string is "Book ABC", the algo should return "Book" since the pattern says Book* which means anything after Book comes, return Book*.

  2. if the input string "Book Two states", then it should return exact macth i.e "Book Two states" from the saved string.

It seems to be little complecated...string.Contains..substring..is not giving me any expected results..

Thanks for replying..

 
0
 

Hi,

I have written a small console application. But still not that does'n solve my problem.
Please see the code below (Please read the inline comment as well):

static void Main(string[] args)
        {
            List<string> lstPatterns = new List<string> 
            { 
                 "Book*",
                 "Book Two*",
                 "Book Two states",
                 "*Two*",
                 "Shedney sheldon",
                 "sydney Book",
                 "Shopoholic*",
                 "Shopoholic part II",
                 "Shopoholic part III"
            };

            // Calls to GetMatchString()

            GetMatchString("Book", lstPatterns); // Out put should be "Book" - Output is Book

            GetMatchString("Book Two", lstPatterns);  // Out put should be "Book Two or Book Two states" but output is Book

            GetMatchString("Book Two states", lstPatterns); // Out put should be "Book Two states"  but output is Book

            GetMatchString("Two", lstPatterns); // Out put should be "Book Two or Book Two states"

            // the last call GetMatchString() also gives this exception
            // Exception: parsing "*Two*" - Quantifier {x,y} following nothing.

            Console.ReadLine();

        }
        private static void GetMatchString(string input, List<string> lstPatterns)
        {
            string matchString = string.Empty;

            foreach (string s in lstPatterns)
            {

               Match m = Regex.Match(input, s);
                if (m != null && !string.IsNullOrEmpty(m.ToString()))
                {
                    matchString = m.Value.ToString();
                    break;
                }
            }
            Console.WriteLine(matchString);
        }

Please see the comment inline on above GetMatchString() calls. I think Regex would be the best option to get the solution. But am not good at that. Can some one help me?

Regards,

 
0
 

try this>>

    static void Main(string[] args)
        {

            List<string> lstPatterns = new List<string> 
            { 
                 "Book*",
                 "Book Two*",
                 "Book Two states",
                 "*Two*",
                 "Shedney sheldon",
                 "sydney Book",
                 "Shopoholic*",
                 "Shopoholic part II",
                 "Shopoholic part III"
            };
            // Calls to GetMatchString()
            GetMatchString("Book", lstPatterns); // Out put should be "Book" - Output is Book
            GetMatchString("Book Two", lstPatterns);  // Out put should be "Book Two or Book Two states" but output is Book
            GetMatchString("Book Two states", lstPatterns); // Out put should be "Book Two states"  but output is Book
            GetMatchString("Two", lstPatterns); // Out put should be "Book Two or Book Two states"
            // the last call GetMatchString() also gives this exception
            // Exception: parsing "*Two*" - Quantifier {x,y} following nothing.
            Console.ReadLine();


        }
        private static void GetMatchString(string input, List<string> lstPatterns)
        {
            string matchString = string.Empty;
           //Match m = Regex.Match(input, s);
                //if (m != null && !string.IsNullOrEmpty(m.ToString()))
                //{
                //    matchString = m.Value.ToString();
                //    break;
                //}

                var query = (from n in lstPatterns.AsEnumerable()
                             where n.Contains(input.Trim())
                             select n).ToArray();

                foreach (var item in query)
                {
                    Console.WriteLine(item.ToString());
                }

                Console.WriteLine("\n");

        }
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article