I'm getting inconsistent/error results for a Regex MatchCollection using C#;
In my first example, the MatchCollection works. Here is the code that works.
string temp1 = "<td nowrap CLASS=\"sportPicksBorderL2\" style=\"width: 150px;\"> \n<B>deGrom, J</B> \n</td>";
Match inputa = Regex.Match(temp1, @"<B>(.*?)</B>", RegexOptions.IgnoreCase);
MatchCollection inputb = Regex.Matches(temp1, @"<B>(.*?)</B>", RegexOptions.IgnoreCase);
string result1 = inputb[0].Groups[1].Value.ToString(); // Value="deGrom, J"
It finds and formats the correct result: "deGrom,J".
But when I used a very similar input, I get an error "System.ArgumentOutOfRangeException". Here is the code for the code that does not work.
string temp2 = "<td nowrap CLASS=\"sportPicksBorderL\"> \n<B>\nScherzer, M \n</B> \n</td>";
Match inputc= Regex.Match(temp2, @"<B>(.*?)</B>", RegexOptions.IgnoreCase);
MatchCollection inputd = Regex.Matches(temp2, @"<B>(.*?)</B>", RegexOptions.IgnoreCase);
string result2 = inputd[0].Groups[1].Value.ToString();
Here is the full error code: // System.ArgumentOutOfRangeException // HResult = 0x80131502 // Message = Specified argument was out of the range of valid values. //Parameter name: i
What is the correct regex pattern to use? Is the tag handled differently?
Thanks
it because the second string has a newline \n
you need add option RegexOptions.Singleline
string temp2 = "<td nowrap CLASS=\"sportPicksBorderL\"> \n<B>\nScherzer, M \n</B> \n</td>";
Match inputc= Regex.Match(temp2, @"<B>(.*?)</B>", RegexOptions.IgnoreCase);
MatchCollection inputd = Regex.Matches(temp2, @"<B>(.*?)</B>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
string result2 = inputd[0].Groups[1].Value.ToString();
Console.WriteLine(result2);
User contributions licensed under CC BY-SA 3.0