Question on Regex.Split

F

Frank Oquendo

I have the following code:

string pattern =
@"(\{)|(})|(\()|(\))|(\[)|(])|(\^)|(\*)|(/)|(-)|(\+)|(%)";
Regex regex = new Regex(pattern);
string input = "QTY * ESTIMATED COST + 2";
string[] tokens = regex.Split(input);
for (int i = 0; i != tokens.Length; i++)
{
Console.WriteLine("Token {0} = {1}", i, tokens.Trim());
}
Console.ReadLine();

According to the documentation on the Split function, the returned
string array will include the captures if I use capturing groups. IOW, I
should get an array containing 5 elements. Instead I'm only getting
three (QTY, ESTIMATED COST and 2). What am I doing wrong?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
F

Frank Oquendo

Never mind. I changed my pattern to the following and it works as
expected now.

string pattern = @"([-\+%\*\^\{}\(\)\[\]])";

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top