I am trying to parse a string and split it by some delimiters, also including the delimiters.
For example, from the string if(a>b) write(a);
I want to get if
,(
,a
,>
,b
,)
,write
,(
,a
,)
,;
Here is what I've tried:
string pattern = "(" + String.Join("|", delimiters.Select(d =>Regex.Escape(d)).ToList()) + ")";
List<string> result = Regex.Split(line, pattern).ToList();
It works, but it fails in some cases. If I had the string if(a>0) write("it is positive");
I would not like to get "it
,is
,positive"
(because space is a delimiter), but "it is positive"
. How can I do this?