473,320 Members | 2,109 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,320 software developers and data experts.

using Regex to split a string

String line = " I am new to c# ";

How to use Regex to split the string so that It would return a String array
with:

token[0] = "I"
token[1] = "am"
token[2] = "new"
token[3] = "to"
token[4] = "c#"
Please help.

thanks

Nov 16 '05 #1
4 3355

//regex expression to take out any ctrl + somekey values
private System.Text.RegularExpressions.Regex Regx = new
System.Text.RegularExpressions.Regex(@"[\cA-\cZ]");
private char[] _split = { ' ' };

string mystring = " I am new to c# ";
mystring = mystring.Replace(" "," ");
mystring = mystring.Replace(" "," ").Trim();
string[] line = null;

//regx implementation
line = Regx.Replace(mystring,"").ToString().Split(_split) ;
output would be:
line [0] = "I"
line [1] = "am"
line [2] = "new"
line [3] = "to"
line [4] = "c#"

Cheers!

Rob
"dchan1" wrote:
String line = " I am new to c# ";

How to use Regex to split the string so that It would return a String array
with:

token[0] = "I"
token[1] = "am"
token[2] = "new"
token[3] = "to"
token[4] = "c#"
Please help.

thanks

Nov 16 '05 #2
BTW:

If you are just using a string veriable and not needing to take care of
regualar expression issues, you should just use the split() from the string

string mystring = " I am new to c# ";
char[] _split= " ".ToCharArray();

mystring = mystring.Replace(" "," ");
mystring = mystring.Replace(" "," ").Trim();

string[] line = mystring.ToString().Split(_split) ;

output would be:
line [0] = "I"
line [1] = "am"
line [2] = "new"
line [3] = "to"
line [4] = "c#"

Cheers!

Rob

"dchan1" wrote:
String line = " I am new to c# ";

How to use Regex to split the string so that It would return a String array
with:

token[0] = "I"
token[1] = "am"
token[2] = "new"
token[3] = "to"
token[4] = "c#"
Please help.

thanks

Nov 16 '05 #3
dchan1,
Have you tried:

String lines[] = RegEx.Split(line.Trim(), "\s+");

Which says to removing leading & trailing spaces, when split the result
based on one or more white space characters.

Hope this helps
Jay

"dchan1" <dc*******@hotmail.com> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...
String line = " I am new to c# ";

How to use Regex to split the string so that It would return a String
array
with:

token[0] = "I"
token[1] = "am"
token[2] = "new"
token[3] = "to"
token[4] = "c#"
Please help.

thanks

Nov 16 '05 #4
Doh!
Which says to removing leading & trailing spaces, when split the result <grammar check>
Which says to remove leading & trailing spaces, then split the result
</grammar check

Some days I worry about me... :-)

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uc**************@TK2MSFTNGP15.phx.gbl... dchan1,
Have you tried:

String lines[] = RegEx.Split(line.Trim(), "\s+");

Which says to removing leading & trailing spaces, when split the result
based on one or more white space characters.

Hope this helps
Jay

"dchan1" <dc*******@hotmail.com> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...
String line = " I am new to c# ";

How to use Regex to split the string so that It would return a String
array
with:

token[0] = "I"
token[1] = "am"
token[2] = "new"
token[3] = "to"
token[4] = "c#"
Please help.

thanks


Nov 16 '05 #5

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

Similar topics

5
by: Blue Ocean | last post by:
In short, it's not working right for me. In long: The program is designed to read numbers from an accumulator and speak them out loud. Unfortunately, the class that contains the method to...
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...
5
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
9
by: Ravi Singh (UCSD) | last post by:
Hello all I have a huge string that I need to parse Key <Delim1> Value <Delim2> Key <Delim1> Value <Delim2> Key <Delim1> Value <Delim3> Key <Delim1> Value <Delim2> Key <Delim1> Value...
5
by: Bob | last post by:
I think this is very simple but I am having difficult doing it. Basically take a comma separated list: abc, def, ghi, jk A list with only one token does not have any commas: abc The first...
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;}")...
3
by: Michael D Murphy | last post by:
Hi, I would like to know how to use Regular Expressions to iteratively return and print the items between the colons in the following string to say the console.. Any help would be appreciated....
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
7
by: =?Utf-8?B?amFj?= | last post by:
Hi, I have problems with following code and don’t find the bug : // Set ArrayList aArray = new ArrayList(); regStr = new Regex(@"\?)*(\d+)\]"); if(text != null && regStr.IsMatch(text))...
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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

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.