REGEX

S

shofu_au

HI Group,

I am trying to use the powerfull regex to split an input string.

Todate I have been able to split simple strings that have ethier space
or comma seperators.

I have now come across a string now that has both space and comma
seperator and the combination of space and comma as a single
seperator.

For the following string

Field1<space>Field2<comma>Field3<space><comma>Field4

What would be the correct regex expression to split the string into
four fields.

Field1
Field2
Field3
Field4

Also what would be a good reference book or web page that explains
regex for C# programmers.

Thanks

Mark
 
M

Moty Michaely

HI Group,

I am trying to use the powerfull regex to split an input string.

Todate I have been able to split simple strings that have ethier space
or comma seperators.

I have now come across a string now that has both space and comma
seperator and the combination of space and comma as a single
seperator.

For the following string

Field1<space>Field2<comma>Field3<space><comma>Field4

What would be the correct regex expression to split the string into
four fields.

Field1
Field2
Field3
Field4

Also what would be a good reference book or web page that explains
regex for C# programmers.

Thanks

Mark

Dear Mark,

here are some sites that can guide you through regular expressions:
http://www.codeproject.com/dotnet/RegexTutorial.asp
http://www.regular-expressions.info/

Moty
 
A

Alberto Poblacion

I am trying to use the powerfull regex to split an input string.

Todate I have been able to split simple strings that have ethier space
or comma seperators.

I have now come across a string now that has both space and comma
seperator and the combination of space and comma as a single
seperator.

For the following string

Field1<space>Field2<comma>Field3<space><comma>Field4

Using RegEx is overkill for this purpose. You can use the String.Split
function to achieve what you want:

string stringToSplit="Field1 Field2,Field3 ,Field4";
string[] fragments=stringToSplit.Split(new char[]{' ',','},
StringSplitOptions.RemoveEmptyEntries);
 
J

Jesse Houwing

* Alberto Poblacion wrote, On 13-5-2007 14:21:
I am trying to use the powerfull regex to split an input string.

Todate I have been able to split simple strings that have ethier space
or comma seperators.

I have now come across a string now that has both space and comma
seperator and the combination of space and comma as a single
seperator.

For the following string

Field1<space>Field2<comma>Field3<space><comma>Field4

Using RegEx is overkill for this purpose. You can use the String.Split
function to achieve what you want:

string stringToSplit="Field1 Field2,Field3 ,Field4";
string[] fragments=stringToSplit.Split(new char[]{' ',','},
StringSplitOptions.RemoveEmptyEntries);

Though, should you want to know how to do this with RegEx:

string stringToSplit="Field1 Field2,Field3 ,Field4";
string[] fragments= Regex.Split(stringToSplit, @"[ ,]+");

It's quite similar, but probably a little bit slower.

Jesse
 

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