I have strings that are similar to the ones below:
string str1 = "test_20150505";
string str2 = "test_20150505_yts";
string str3 = "test_all";
string str4 = "test";
The below regex code extracts date (20150505) for string str1 and for string str3 & string str4 returns empty, which is also fine.
However, what do I have to change so as to extract date for string str2?
string d = Regex.Match(str1, "^(?:.*_)?([0-9]{8})(?:\\..*)?$").Groups[1].Value.ToString();


(?:\\..*)?part in your regex? Non of yours examples dates ends with..