I’m trying to parse instructions passed in. I want to use regex to validate each substring.
How can I make sure a string ends with a comma? My regex for memory(mips) is also not working.
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;
}
example of how to load from memory
lw $t7,248($t2)
I am not sure what you mean about a string ending with a comma. As far as I can tell you can just write
@"\$t[0-9],".Your memory regex doesn’t match because you have
[s-t]\b[0-9]. Since s and t and 0 through 9 are all word characters there cannot be a word boundary between them. Also you have an unescaped closing parenthesis. This@"\d+\(\$[st][0-9]\)"will work.If your operand list is simply delimited by commas, then split the string on commas and verify each one
and your regexes need to be anchored at the beginning and end, like this
and so on.