【发布时间】:2012-03-12 02:11:57
【问题描述】:
我正在尝试解析传入的指令。我想使用正则表达式来验证每个子字符串。 如何确保字符串以逗号结尾?我的内存正则表达式(mips)也不起作用。
public static OperandType GetRegisterType(this string source)
{
if (Regex.IsMatch(source, @"\$t[0-9]"))
return OperandType.Temporary; // $t0 - $t9
if (Regex.IsMatch(source, @"\$s[0-9]"))
return OperandType.Store; // $s0 - $s9
if (Regex.IsMatch(source, @"\$k[0-1]"))
return OperandType.OSReserved; // $k0 - $k1
if (Regex.IsMatch(source, @"[-+]?\b\d+\b"))
return OperandType.Constant;
if (Regex.IsMatch(source, @"\$zero"))
return OperandType.Special;
if (Regex.IsMatch(source, @"[a-zA-Z0-9]+\b\:"))
return OperandType.Label;
if (Regex.IsMatch(source, @"\d+\b\(\$[s-t]\b[0-9])"))
return OperandType.Memory;
return OperandType.Invalid;
}
如何从内存中加载的示例
lw $t7,248($t2)
【问题讨论】:
-
为什么需要正则表达式? source.EndsWith(",") 会做你想做的事,而且很好读。
-
我需要确保整个字符串适合表格。即临时 => $t0 - $t9
-
请解释一下我如何确保字符串以逗号结尾? 作为您的示例
lw $t7,248($t2)末尾没有逗号。 -
@Borodin 每个操作数都以逗号结尾,除了最后一个。在该示例中,只有 2 个操作数,lw 考虑运算符并以不同方式处理
-
那我觉得你应该用
String.Split在逗号处分割操作数字符串,分别验证每一块。