var lexer = new LexState(); lexer.Patterns.Add("email", @"\b[\w\.]+@[\w\.]+\.\w+\b"); lexer.Input = "[email protected]"; while (!lexer.IsEof) { var token = lexer.NextToken(); if (token.Type == "email") { Console.WriteLine("Email address found: " + token.Value); } }
var lexer = new LexState(); lexer.Patterns.Add("keyword", @"\b(if|else|while|for)\b"); lexer.Patterns.Add("identifier", @"[a-zA-Z_]\w*"); lexer.Patterns.Add("number", @"\d+"); lexer.Input = "for i = 1 to 10 do\n" + " if i % 2 == 0 then\n" + " print(i)\n" + " end\n" + "end"; while (!lexer.IsEof) { var token = lexer.NextToken(); Console.WriteLine(token); }This example defines patterns to match keywords, identifiers, and numbers commonly found in programming languages. It then parses a sample program to produce a stream of tokens. The LexState library is not an official package or library from Microsoft, but rather a third-party library that can be installed using NuGet or other package managers.