I'm doing a tag-like string match function where function checks is string contains any of possible words while maintaining their order, at least per tag. I figured out it will be best to precreate list of possibilities and on check simply see if string contains each of required combinations
Maybe code will make it more clear.
List<List<string[]>> tags;
List<string[]> innerList;
List<List<string>> combinationsList;
public void Generate(string pattern)
{
// i will add whitespace removal later so it can be ", " instead of only ","
foreach (string tag in pattern.Split(','))
{
innerList = new List<string[]>();
foreach (var varyword in tag.Split(' '))
{
innerList.Add(varyword.Split('|'));
}
}
// atm i lack code to generate combinations in form of List<List<string>>
// and drop them into 'combinationsList'
}
// the check function will look something like isMatch = :
public bool IsMatch(string textToTest)
{
return combinationsList.All(tc => tc.Any(c => textToTest.Contains(c)));
}
So for example pattern:
"old|young john|bob, have|posses dog|cat"
- tags:
- List_1:
- {old, young}
- {john, bob}
- List_2
- {have, posses}
- {dog, cat}
- List_1:
So combinationsList will have:
- combinationsList :
- List_1
- "old john"
- "old bob"
- "young john"
- "young bob"
- List_2
- "have dog"
- "have cat"
- "posses dog"
- "posses cat"
- List_1
So results will be:
- old bob have cat = true, contains List_1:"old bob" and List_2:"have cat"
- young john have car = false, contains List_1:"young john" but doesn't contain any of List_2 combinations
I cannot figure out how to iterate collection to get those combinations and how to get the combination per iteration. Also i cannot mess up the order so old john will not be also generated as john old.
Please note that any of "variant words" in pattern may have more than 2 variants like for example "dog|cat|mouse"