如何实现字典中没有的单词必须显示错误?
How do I implement words that are not in dictionary must shown with error?
这是一个使用自定义正则表达式实现字典的程序,它标记了所有输入的每个字符串。现在我希望与任何正则表达式不匹配的字符串必须显示在 "not in grammar" 行中。我找不到任何类型的解决方案。
static void Main(string[] args)
{
string StringRegex = "\"(?:[^\"\\]|\\.)*\"";
string IntegerRegex = @"[0-9]+";
string CommentRegex = @"//.*|/\*[\s\S]*\*/";
string KeywordRegex = @"\b(?:astart|ainput|atake|aloop|batcommand|batshow|batprint|batmult|batadd|batsub|batdiv|batif|batelse|batgo|batend|till|and)\b";
string DataTypeRegex = @"\b(?:int|string)\b";
string IdentifierRegex = @"[a-zA-Z]";
string ParenthesisRegex = @"\(|\)";
string BracesRegex = @"\{|\}";
string ArrayBracketRegex = @"\[|\]";
string PuncuationRegex = @"\;|\:|\,|\.";
string RelationalExpressionRegex = @"\>|\<|\==";
string ArthimeticOperatorRegex = @"\+|\-|\*|\/";
string WhitespaceRegex = @" ";
Dictionary<string, string> Regexes = new Dictionary<string, string>()
{
{"String", StringRegex},
{"Integer", IntegerRegex },
{"Comment", CommentRegex},
{"Keyword", KeywordRegex},
{"Datatype", DataTypeRegex },
{"Identifier", IdentifierRegex },
{"Parenthesis", ParenthesisRegex },
{"Brace", BracesRegex },
{"Square Bracket", ArrayBracketRegex },
{"Puncuation Mark", PuncuationRegex },
{"Relational Expression", RelationalExpressionRegex },
{"Arithmetic Operator", ArthimeticOperatorRegex },
{"Whitespace", WhitespaceRegex }
};
string input;
input = Convert.ToString(Console.ReadLine());
var matches = Regexes.SelectMany(a => Regex.Matches(input, a.Value)
.Cast<Match>()
.Select(b =>
new
{
Value = b.Value + "\n",
Index = b.Index,
Token= a.Key
}))
.OrderBy(a => a.Index).ToList();
for (int i = 0; i < matches.Count; i++)
{
if (i + 1 < matches.Count)
{
int firstEndPos = (matches[i].Index + matches[i].Value.Length);
if (firstEndPos > matches[(i + 1)].Index)
{
matches.RemoveAt(i + 1);
i--;
}
}
}
foreach (var match in matches)
{
Console.WriteLine(match);
}
Console.ReadLine();
}
标识符正则表达式应更改为
var IdentifierRegex = @"\b[a-zA-Z]\b";
然后,asdasdas
将不匹配,您将能够测试空结果,例如。
if (matches.Count == 0)
Console.WriteLine("Not in grammar");
else
{ ... }
这是一个使用自定义正则表达式实现字典的程序,它标记了所有输入的每个字符串。现在我希望与任何正则表达式不匹配的字符串必须显示在 "not in grammar" 行中。我找不到任何类型的解决方案。
static void Main(string[] args)
{
string StringRegex = "\"(?:[^\"\\]|\\.)*\"";
string IntegerRegex = @"[0-9]+";
string CommentRegex = @"//.*|/\*[\s\S]*\*/";
string KeywordRegex = @"\b(?:astart|ainput|atake|aloop|batcommand|batshow|batprint|batmult|batadd|batsub|batdiv|batif|batelse|batgo|batend|till|and)\b";
string DataTypeRegex = @"\b(?:int|string)\b";
string IdentifierRegex = @"[a-zA-Z]";
string ParenthesisRegex = @"\(|\)";
string BracesRegex = @"\{|\}";
string ArrayBracketRegex = @"\[|\]";
string PuncuationRegex = @"\;|\:|\,|\.";
string RelationalExpressionRegex = @"\>|\<|\==";
string ArthimeticOperatorRegex = @"\+|\-|\*|\/";
string WhitespaceRegex = @" ";
Dictionary<string, string> Regexes = new Dictionary<string, string>()
{
{"String", StringRegex},
{"Integer", IntegerRegex },
{"Comment", CommentRegex},
{"Keyword", KeywordRegex},
{"Datatype", DataTypeRegex },
{"Identifier", IdentifierRegex },
{"Parenthesis", ParenthesisRegex },
{"Brace", BracesRegex },
{"Square Bracket", ArrayBracketRegex },
{"Puncuation Mark", PuncuationRegex },
{"Relational Expression", RelationalExpressionRegex },
{"Arithmetic Operator", ArthimeticOperatorRegex },
{"Whitespace", WhitespaceRegex }
};
string input;
input = Convert.ToString(Console.ReadLine());
var matches = Regexes.SelectMany(a => Regex.Matches(input, a.Value)
.Cast<Match>()
.Select(b =>
new
{
Value = b.Value + "\n",
Index = b.Index,
Token= a.Key
}))
.OrderBy(a => a.Index).ToList();
for (int i = 0; i < matches.Count; i++)
{
if (i + 1 < matches.Count)
{
int firstEndPos = (matches[i].Index + matches[i].Value.Length);
if (firstEndPos > matches[(i + 1)].Index)
{
matches.RemoveAt(i + 1);
i--;
}
}
}
foreach (var match in matches)
{
Console.WriteLine(match);
}
Console.ReadLine();
}
标识符正则表达式应更改为
var IdentifierRegex = @"\b[a-zA-Z]\b";
然后,asdasdas
将不匹配,您将能够测试空结果,例如。
if (matches.Count == 0)
Console.WriteLine("Not in grammar");
else
{ ... }