473,326 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Question on Regex.Split

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[i].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)
Nov 15 '05 #1
2 1408
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)
Nov 15 '05 #2

Wrap the entire expression in (...) instead of each alternative:

(\{|}|\(|\)|\[|]|\^|\*|/|-|\+|%)
Brian Davis
www.knowdotnet.com

"Frank Oquendo" <fr****@acadxpin.com> wrote in message
news:Ot*************@TK2MSFTNGP11.phx.gbl...
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[i].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)

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: H | last post by:
This is kind of an followup on oneof my previous questions, and it has with RegEx to do. I have a string containing of several words. What would a good regex expression looklike to get one match...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
6
by: Craig Buchanan | last post by:
I have a string in the format "name" <address> that i would like to split into an array of two values. name should be the first value, address the second value. what does my regex pattern need to...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
3
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}")...
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
7
by: Jordi Rico | last post by:
Hi, I know I can split a string into an array doing this: Dim s As String()=Regex.Split("One-Two-Three","-") So I would have: s(0)="One" s(1)="Two"
3
by: =?Utf-8?B?bWFnZ2ll?= | last post by:
hi, I've been working getting a file parsed out using Regex. There's something I don't understand. When I define the pattern for my fields in my file, I am telling regex to grab those fields (...
1
by: mad.scientist.jr | last post by:
I am working in C# ASP.NET framework 1.1 and for some reason Regex.Split isn't working as expected. When trying to split a string, Split is returning an array with the entire string in element ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.