Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi All,
I have a following string,
string strVal = "WHEN table.column1 = 'ABC' AND table.column2 = 'xyz' AND table.column3 IN ( 'pqr','NON' )  THEN 'Code Project'";


I wanna generate substrings from above string and store its respective value
for eg:
column1="ABC"; column2="xyz" column3 contains two values and i want to store it in array like column3[]={"pqr","NON"}...got struck ! pls help!!

Regards,
DevD
Posted
Updated 12-Nov-11 8:48am
v2

What you want to do is called tokenizing.

The best way to do this would probably be with regular expressions. If you don't know what they are yet, well... I want you to remember where you were on November 12, 2011, because your life is about to change.

The 30 Minute Regex Tutorial[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 12-Nov-11 19:52pm    
Good point, a 5.
--SA
nadeva 13-Nov-11 14:42pm    
Thanks Yvan R..I tried with reg expressions.nd it worked quite fine..nd ur link was a very good article (abt 30 min regex tutorial)..
Hi,

Have you tried Regex?

Something like

C#
Regex r = new Regex(@"((?'tab'\w+)\.(?'col'\w+)(\s*=\s*\'(?'val'\w+)\'|\s+IN\s*\((?'item'\s*\'(?'val'\w+)\'\s*,?)+\)))");
var matches = r.Matches(strVal);
string column1 = null, column2 = null;
string[] column3 = null;
foreach (Match m in matches)
{
	Group col = m.Groups["col"];
	if (col.Success)
	{
		switch (col.Value)
		{
			case "column1":
				column1 = m.Groups["val"].Value;
				break;
			case "column2":
				column2 = m.Groups["val"].Value;
				break;
			case "column3":
				column3 = m.Groups["val"].Captures.Cast<capture>().Select(c => c.Value).ToArray();
				break;
		}
	}
}
</capture>


Sorry for bad style, ideafixxxer
 
Share this answer
 
v2
Comments
nadeva 13-Nov-11 14:44pm    
Thanq vry mch ideafixxxer...Now i understand the importance of regex..u made my day!!!:)

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