Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to get contain start with [ and end with ] only from string


[ANC 2 DATE] = [yes] AND [AllIFADone] = [YES] AND

i want out out in array

like
[ANC 2 DATE]
[yes]
[AllIFADone]

other should get ignore
Posted

Member-515487's solution will work, but the use of Balancing Groups is a better idea:
(?<sb>\[).*?(?<-sb>\])
 
Share this answer
 
This can be done without using RegEx:
C#
char[] splitChars = new[] { '[' };

string anc = @"[ANC 2 DATE] = [yes] AND [AllIFADone] = [YES] AND";

string[] ancSplit = anc.Split(splitChars, StringSplitOptions.RemoveEmptyEntries)
    .Select(strx => ('[' + strx.Substring(0, strx.IndexOf(']') + 1))).ToArray();
However, I am not claiming that doing it this way is better than using a RegEx.

Sample result:
C#
? ancSplit
{string[4]}
    [0]: "[ANC 2 DATE]"
    [1]: "[yes]"
    [2]: "[AllIFADone]"
    [3]: "[YES]"
 
Share this answer
 
C#
string str = "[ANC 2 DATE] = [yes] AND [AllIFADone] = [YES] AND ";
           var pattern = @"(\[(?:.*?)\])";

           MatchCollection mcol = Regex.Matches(str, pattern);

           foreach (Match m in mcol)
           {
               Console.WriteLine(m);
           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900