tags:

views:

132

answers:

3

I'd like to split a string such as

"[1-5]?3456[2-5][4-D]"

to

array[0] = "[1-5]"
array[1] = "?"
array[2] = "3"
array[3] = "4"
array[4] = "5"
array[5] = "6"
array[6] = "[2-5]"
array[7] = "[4-D]"

Can anybody tell me if that's possible with a regex that splits?

I got three elements "3" a letter (which can be 1-9 and A-F, "?" a whitecard, "[1-5]" a range (same 1-9 + A-F)

Edit: Examples that match are

"[1-5]?3456[2-5][4-D]"

"?4A9[1-F]?[A-D]1"

"12459987"

"[1-F][1-F][1-F][1-F][1-F][1-F][1-F][1-F]"
A: 

Hmm...

\[[^]]*\]|.
richardtallent
splits every character single
Kai
Escapes missed, I corrected it.
richardtallent
+7  A: 

Tested with Expresso:

(\[[^]]+\])|.

To use this expression to get the splits, you can use the following code:

var input = "[1-5]?3456[2-5][4-D]";
var pattern = new Regex(@"(\[[^]]+\])|(.)", 
                        RegexOptions.CultureInvariant | RegexOptions.Compiled);

IEnumerable<string> parts = from m in pattern.Matches(input).Cast<Match>()
                            select m.Captures[0].Value;
scwagner
The difference between my answer and Richard's is that his will return an empty bracket pair together as "[]" whereas mine will split it into 2 results "[","]".
scwagner
pattern is unknown vs tells me...how can that be?
Kai
@Kai: code was updated, try it again
Ahmad Mageed
Upvoted yours, since you beat me to a fully-functional answer. However, right now the expression has some extra backslashes in it (from before you indented it into a code block). I did think the OP would probably rather have an empty [] set than individual characters, but both solutions have merit depending on the use case.
richardtallent
A: 

Try this:

string pattern = @"(\[[1-9A-F?]-[1-9A-F?]\])|[1-9A-F?]";
string input = "[1-5]?3456[2-5][4-D]";
MatchCollection matches = Regex.Matches(input, pattern);

foreach (Match m in matches)
    Console.WriteLine(m.Value);
Anton Hansson