Matt Zafeiriou
Matt Zafeiriou

Reputation: 1729

Split string using multiple strings with the separators in the final array

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

Answers (1)

ΩmegaMan
ΩmegaMan

Reputation: 31576

Use Regular expressions to parse out the text. By using these rules on creating a match of text.

  • Any consecutive number
  • Any consecutive text
  • An individual separator such as + or - or =. Which is a set in regex speak
  • Ignore any spaces.

The result of from Regex.Matches is the output tokenized:

enter image description here

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.

enter image description here

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

Related Questions