tags:

views:

37

answers:

3

Let's say you need to split a string by various delimiters including newline (/r, /n) and a few other 'special' character strings.

For example:

This is a sample %%% text &&& that I would 
like to split %%% into an array.

I would like the following in the resulting string array (contents via index)

[0]This is a sample
[1]text
[2]that I would
[3]like to split
[4]into an array.

I would like to use C# Regex.Split() function. What is the regex expression to match on all of my delimiters?

Thanks in advance

A: 

I don't have this solution ready. You can create a class and compare against it. You may be served by:

http://www.regexbuddy.com/

Curtis White
+3  A: 

%%%|&&& should do it.

Dave
Also, you could use @"[\r\n...insert characters here...]+" to eliminate splitting multiple times if separators are neighbors. Or @"[\r\n]" to split even if neighbors.
Fredrik Johansson
Thanks I think I'm almost there. I'm only having an issue where there is an extra (blank) entry in my string array for these delimiters.
Arthur Frankel
I went with Eric's solution but I'm sure I just had this wrong somewhere. Thanks Dave and Fredrik.
Arthur Frankel
No worries, I prefer Erics answer anyway :)
Dave
+4  A: 

Just FYI, the vanilla String.Split() method has an overload which accepts an array of strings to use as delimiters. Here's a link to MSDN's page describing it.

Eric
Thanks! I can't believe I missed this one. This solved my extra blank entry issue.
Arthur Frankel
You're welcome. Happy to help!
Eric