Reputation: 5910
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]"
Upvotes: 1
Views: 268
Reputation: 4005
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;
Upvotes: 8
Reputation: 2171
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);
Upvotes: 0