我有这个分解字符串输入的 Tokinizer 类:
public class Tokinizer
{
public static Regex r = new Regex(
"(?<Equals>=)" +
"|(?<Plus>\\+)" +
"|(?<Minus>\\-)" +
"|(?<Divide>\\/)" +
"|(?<Multiply>\\*)" +
"|(?<Exclamation>\\!)" +
"|(?<GreaterThan>\\>)" +
"|(?<SmallerThan>\\<)" +
"|(?<OpenParenthesis>\\()" +
"|(?<CloseParenthesis>\\))" +
"|(?<OpenBracket>\\[)" +
"|(?<CloseBracket>\\])" +
"|(?<OpenBrace>\\{)" +
"|(?<CloseBrace>\\})" +
"|(?<Colon>\\:)" +
"|(?<SemiColon>\\;)" +
"|(?<Comma>\\,)" +
"|(?<FullStop>\\.)" +
"|(?<Quatation>\\\")" +
"|(?<Char>[a-zA-Z0-9])" +
"|(?<space>\\s+)", RegexOptions.ExplicitCapture);
public static void GetTokens(string input)
{
foreach (var t in r.Matches(input))
{
Console.WriteLine("Named Group : Token Value");
}
}
我想打印捕获组的名称以及匹配列表中的值,这可能吗?
例如,当我输入“var++”时,它应该输出:
Char : v
Char : a
Char : r
Plus : +
Plus : +