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 YYY
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 Book ABC XYZ.
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,
VulpesPosted Feb 16, 2013, 6:34 AM
The output should be:
Sumitra PaulPosted Feb 19, 2013, 11:00 PM
Sumitra PaulPosted Feb 16, 2013, 5:02 AM
Hi,
Thanks a lot for the reply.
i was trying to write one more condition for the search but could'n write :-(. i.e if the saved wildcard has * in the middle somewhere.
Say, if saved wildcard string is "Book * xyz"
and
1. The input string is "Book abc yyy xyz" :- the output should be Book * xyz.
Can we write wildcard search for some complex estrics in between as well?? like
say, if the saved wildcard string is "Book * abc * xyz"
and
1. The input string is "Book nnn yyy abc kkk xyz" : the output should be "Book * abc * xyz".
Thanks again.
VulpesPosted Feb 12, 2013, 2:29 PM
However, try this:
}
The output should be:
Sumitra PaulPosted Feb 12, 2013, 1:09 PM
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) lstPatterns = new List
{
List
{
"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();
} lstPatterns)
private static void GetMatchString(string input, List
{
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,
Sumitra PaulPosted Feb 12, 2013, 8:47 AM
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..
Rishikesh Kumar SinghPosted Feb 12, 2013, 7:29 AM
Contains method takes one string type argument and returns boolean value..If a string contains a substring then it returns true otherwise returns false.
for example:-
string s1 = "The quick brown fox jumps over the lazy dog";
string s2 = "fox";
bool b;
b = s1.Contains(s2);
Console.WriteLine("Is the string, s2, in the string, s1?: {0}", b);
if you have saved your strings in database table..then by using the query you can get it
Select str from Str_details where str LIKE \"%" + txt_subString.Text + "%\""
If it helps you then mark it as accepted answer otherwise let me know your exact
requirement that where you are storing your strings, (in array of strings or in Collection or
in database)
I will send you the complete code.....