Reputation: 1729
I have been searching and found that I can split a string for example "x += 10" using multiple strings so I get the result { "x", "10" } but I want to have the separators in the final array too, so it will be { "x", " +", "= ", "10" } using the separators " +" and "= ". The code I used is var words = code.Split(Actions.available, StringSplitOptions.RemoveEmptyEntries);
Upvotes: 0
Views: 236
Reputation: 31576
Use Regular expressions to parse out the text. By using these rules on creating a match of text.
+
or -
or =
. Which is a set in regex speakThe result of from Regex.Matches
is the output tokenized:
Code
var input = "x += 10";
var pattern = @"(\w+|[-=+])";
Regex.Matches(input, pattern)
.OfType<Match>()
.Select(mt => mt.Value);
Alternate With Regex Split
If one uses Regex.Split
with the same pattern, it splits on everything as we specified as before, but also puts adds in the spaces due to its design.
But to compensate with the addition of a linq extension call on the resulting list, we can remove the spaces and achieve the same answer.
Regex.Split(input, @"(\w+|[-=+])")
.Where(str => !string.IsNullOrWhiteSpace(str))
result is { "x", "+", "=", "10" }
Upvotes: 3