[英]Trouble with regular expressions parser
Im trying to create a method that checks a string against a regular express and returns a register type(mips). 我试图创建一种方法,该方法根据正则表达式检查字符串并返回寄存器类型(mips)。 The problem is that I can't seem to be able to create the correct regex.
问题是我似乎无法创建正确的正则表达式。 Please take a look and make suggestions.
请看看并提出建议。 Thanks
谢谢
public static RegisterType CheckRegex(this string source)
{
var tempMatch = new Regex("$t0|$t1|$t2|$t3|$t4|$t5|$t6|$t7|$t8|$t9|").Match(source); //$t0 - $t9
if(tempMatch.Length == source.Length)
return RegisterType.Temporary;
var storeMatch = new Regex(@"(^\$s)+[0-9]").Match(source); //$s0 - $s9
if (storeMatch.Length == source.Length)
return RegisterType.Store;
var reservedMatch = new Regex(@"").Match(source); //$k0 - $k1
if (reservedMatch.Length == source.Length)
return RegisterType.OSReserved;
var constantMatch = new Regex(@"0-9").Match(source); //Any integer
if (constantMatch.Length == source.Length)
return RegisterType.Constant;
var memoryMatch = new Regex("").Match(source);
if (memoryMatch.Length == source.Length)
return RegisterType.Memory;
return RegisterType.Invalid;
}
UPDATE: Everything is working now ,excluding my Memory Regex 更新:现在一切正常,不包括我的Memory Regex
public static RegisterType GetRegisterType(this string source)
{
if (Regex.IsMatch(source, @"\$t[0-9]"))
return RegisterType.Temporary; // $t0 - $t9
if (Regex.IsMatch(source, @"\$s[0-9]"))
return RegisterType.Store; // $s0 - $s9
if (Regex.IsMatch(source, @"\$k[0-1]"))
return RegisterType.OSReserved; // $k0 - $k1
if (Regex.IsMatch(source, @"[-+]?\b\d+\b"))
return RegisterType.Constant;
if (Regex.IsMatch(source, @"\$zero"))
return RegisterType.Special;
if (Regex.IsMatch(source, @"[a-zA-Z0-9]+\b\:"))
return RegisterType.Label;
if (Regex.IsMatch(source, @"\d+\b\(\$[s-t]\b[0-9])"))
return RegisterType.Memory;
return RegisterType.Invalid;
}
$
is a special character in regular expression, matches at the end of the line. $
是正则表达式中的特殊字符,在行末匹配。 If you want to match $
literal, use escaping (\\$)
如果要匹配
$
文字,请使用转义(\\$)
As others have said, you need to escape the dollar signs in "$t0|$t1|$t2|$t3|$t4|$t5|$t6|$t7|$t8|$t9|"
正如其他人所说,您需要转义
"$t0|$t1|$t2|$t3|$t4|$t5|$t6|$t7|$t8|$t9|"
的美元符号。 by prefixing them with a backslash. 在它们前面加上反斜杠。 Also, you can write that more concisely as
@"\\$t[0-9]"
. 另外,您可以更简洁地将其写为
@"\\$t[0-9]"
。 That will match a dollar sign followed by 't'
followed by a single digit. 这将匹配一个美元符号,后跟一个
't'
和一个数字。 You've got a trailing pipe character followed by nothing, as well, that can be removed. 您具有结尾的竖线字符,后面也没有任何可以删除的字符。
If your source
is just a register/memory location, you could probably simplify this thing down to something like this: 如果您的
source
只是一个寄存器/内存位置,则可以将其简化为以下内容:
public static RegisterType CheckRegex(this string source)
{
if (Regex.IsMatch(@"\$\t\d")) return RegisterType.Temporary; // $t0 - $t9
if (Regex.IsMatch(@"\$\s\d")) return RegisterType.Store; // $s0 - $s9
if (Regex.IsMatch(@"\$\k\[0-1]")) return RegisterType.OSReserved; // $k0 - $k1
if (Regex.IsMatch(source, @"\d")) return RegisterType.Constant;
// Don't remember the pattern for Memory, if you post an update I can update this
return RegisterType.Invalid;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.