Reputation: 469
I am trying to write an advanced regrex that will split my string in correct places.
Do you remember the old phones (T9), where you could simply do 22022 to write 'bb' I need something similiar.
I got the splitting at the whitespaces(which implement the pause), but the rest is really hard.
For whitespace it is
var l = Regex.Split(a, @"\s");
but i would need following:44204420442 should be 44, 2, 0, 44, 2, 0, 44, 2
so it would split also after the character is changed compared to the previous one.
Thank you in advance!
Upvotes: 2
Views: 115
Reputation: 186678
Instead of complex regular expression you can implement a simple loop:
public static IEnumerable<String> MySplit(String value) {
if (null == value)
throw new ArgumentNullException("value");
char prior = '\0';
StringBuilder sb = new StringBuilder();
foreach (char current in value) {
if (sb.Length != 0 && prior != current) {
yield return sb.ToString();
sb.Clear();
}
if (!Char.IsWhiteSpace(current))
sb.Append(current);
prior = current;
}
if (sb.Length > 0)
yield return sb.ToString();
}
Test:
string value = "44204420442";
string[] parts = MySplit(value).ToArray(); // if you want an array
// 44, 2, 0, 44, 2, 0, 44, 2
Console.Write(String.Join(", ", parts));
string value2 = "44 44 42";
// 44, 44, 4, 2
Console.Write(String.Join(", ", MySplit(value2)));
Upvotes: 1
Reputation: 336138
Instead of splitting, it's easier to collect all the matches:
Regex regexObj = new Regex(@"(\d)\1*");
allMatchResults = regexObj.Matches(subjectString);
You need to look at each Match
object's .Groups[0].Value
to get the entire match.
Upvotes: 4