Im trying to create a method that checks a string against a regular express and returns a register type(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
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;
}
As others have said, you need to escape the dollar signs in
"$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]". That will match a dollar sign followed by't'followed by a single digit. You’ve got a trailing pipe character followed by nothing, as well, that can be removed.