I’m trying to match the text content from the first tag <test>.
For example:
<test>SAMPLE TEXT</test><test>SAMPLE TEXT2</test><test>SAMPLE TEXT3</test>
If I use
("<test>(.*)</test>")`
I got this:
SAMPLE TEXT</test><test>SAMPLE TEXT2</test><test>SAMPLE TEXT3
How to get just the content from the first <test> tag: SAMPLE TEXT?
(.*)is greedy (meaning “everything you can match until you find the last</test>“), you’re looking for the non-greedy version(.*?)(meaning “as little as you can match until you find the very first</test>“).Do however keep in mind the call of Cthulu when thinking about parsing HTML with regex and take a look at this question for a discussion about the best practices for parsing HTML with .NET. Or, if this is XML (not HTML), then by all means, do it the proper (and easy) way with an
XmlReader.