Define a Regex Expression in C# to parse TCL syntax, allowing multiple strings in the token stream including “”, “\””, and “\{“. TCL is very string
Try this. It handles any escaped quotes inside quotes.
@"(?:(""[^\\""]*(?:\\.[^\\""]*)*"")|([\[\]{}();,])|([%*+\-/])|(<<|>>)|([!=]=|[<>]=?)|(!|&&|\|\|)|([&\^|])|([:?])|\s+)"
{
//for example
string line1 = @"[return [file attributes $var -readonly]";
string line2 = @"set x[string map { ""\"""" """"} $var]";
//The original regex is
//string in ""| certain chars| whitespace | all the operators
string pattern = @"(?:(""[^\\""]*(?:\\.[^\\""]*)*"")|([\[\]{}();,])|([%*+\-/])|(<<|>>)|([!=]=|[<>]=?)|(!|&&|\|\|)|([&\^|])|([:?])|\s+)";
//does the tokenizing
string[] regexResult = Regex.Split(line2, pattern, RegexOptions.None);
int cnt = 0;
int arysize = regexResult.Length;
foreach (String str in regexResult)
{
if (str != "")
{
Console.Write(str);
if (cnt < arysize - 2)
Console.Write(", ");
}
cnt++;
}
Console.WriteLine();
}
Output:
set, x, [, string, map, {, "\"", "", }, $var, ]
Refactored:
(?:
( # (1 start)
"
[^\\"]*
(?: \\ . [^\\"]* )*
"
) # (1 end)
| ( [\[\]{}();,] ) # (2)
| ( [%*+\-/] ) # (3)
| ( << | >> ) # (4)
| ( [!=] = | [<>] =? ) # (5)
| ( ! | && | \| \| ) # (6)
| ( [&\^|] ) # (7)
| ( [:?] ) # (8)
| \s+
)
Read more here: Source link
