这是一个使用自定义正则表达式实现字典的程序,它会对输入的每个字符串进行标记。现在我希望与任何正则表达式不匹配的字符串必须显示在"而不是语法"线。我无法遇到任何类型的解决方案。
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();
}
答案 0 :(得分:1)
标识符正则表达式应更改为
asdasdas
然后,if (matches.Count == 0)
Console.WriteLine("Not in grammar");
else
{ ... }
将不匹配,您将能够测试空结果,例如。
getUserAds(user).then(function(response) {
// useMyData
});
function getUserAds {
if( thereIsLocalData ) {
return getLocalData(); // this returns a $q promise
} else {
return getDataDB() // Here I'm returning an $http Promise
}
}
function getDataDB (params) {
return $http.get(host, params, {}); // I'm returning the $http Promise!
}
function getLocalData (params) {
return $q(function(resolve, reject) {
resolve(myLocalData);
}); // return the $q promise!
}
请参阅this IDEONE demo。