Using a regex, I want to be able to get text between multiple DIV tags. For example, the following:
<div>first html tag</div>
<div>another tag</div>
Output:
first html tag
another tag
I use the regex pattern only for my last div tag and skip the first. The code:
static void Main(string[] args)
{
string input = "<div>This is a test</div><div class=\"something\">This is ANOTHER test</div>";
string pattern = "(<div.*>)(.*)(<\\/div>)";
MatchCollection matches = Regex.Matches(input, pattern);
Console.WriteLine("Matches found: {0}", matches.Count);
if (matches.Count > 0)
foreach (Match m in matches)
Console.WriteLine("Inner DIV: {0}", m.Groups[2]);
Console.ReadLine();
}
Conclusion:
Matches found: 1
Internal DIV: this is ANOTHER test
source
share