Reputation: 6547
I would like to split the strings like 2x3y5z
, 4y
, 5x6y
, 7x4z
into separate strings:
"2x3y5z" = { "2x", "3y", "5z" }
"7x4z"= { "7x", "4z" }
My current solution involves Substring
and Replace
and looks quite convoluted. Is it possible to do this in RegEx in a much simpler way ?
Upvotes: 1
Views: 195
Reputation: 2387
What are the rules to split the string? The following regexp assumes you have number from 0 to 9 and after that character from a to z.
string pattern = @"[0-9]{1}[a-z]{1}";
var regexp = new System.Text.RegularExpressions.Regex(pattern);
var matches = regexp.Matches("2x3y5z");
foreach (var match in matches)
{
Debug.WriteLine(match);
}
Upvotes: 3
Reputation: 121710
Provided you want to have groups consisting of a digit then a lowercase letter, cycle through your input with \d[a-z]
and grab the matched text.
Upvotes: 0