I am writing code to extract all the words enclosed within @{{}}@ and {{}}, so far I have searched the web and found the below code which works as expected.
string sampleString = "A @{{Quick}}@ brown @{{fox}}@ jumps @{{over}}@ a lazy {{dog}}.";
List<string> keywordList = new List<string>();
MatchCollection matchedCollection = Regex.Matches(sampleString, @"(@{{(.*?)}}@|{{(.*?)}})");
foreach (Match m in matchedCollection)
{
keywordList.Add(m.ToString());
}
Above code works fine, it gives me 4 items listed below and that is correct.
- @{{Quick}}@
- @{{Fox}}@
- @{{Over}}@
- {{dog}}
But, the problem arises when the word is not properly enclosed in the brackets/pattern. For example, if I have improperly formatted string like below, I'll get incorrect result.
string sampleString = "A @{{Quick}}@ brown @{{fox jumps @{{over}}@ a lazy {{dog}}.";
Code with above string input will give me three items in list.
Current Result:
- @{{Quick}}@
- @{{fox jumps @{{over}}@
- {{dog}}
Expected Result
- @{{Quick}}@
- @{{over}}@
- {{dog}}
Any suggestion to correct this would be really appreciated.