MatchCollection Result Not Consistent

0

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;\">&nbsp;\n<B>deGrom, J</B>&nbsp;\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\">&nbsp;\n<B>\nScherzer, M \n</B>&nbsp;\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

regex
pattern-matching
match
asked on Stack Overflow Feb 27, 2021 by Trey Balut

1 Answer

1

it because the second string has a newline \n you need add option RegexOptions.Singleline

string temp2 = "<td nowrap CLASS=\"sportPicksBorderL\">&nbsp;\n<B>\nScherzer, M \n</B>&nbsp;\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);
answered on Stack Overflow Feb 27, 2021 by uingtea

User contributions licensed under CC BY-SA 3.0