Next In GetFirstParagraph, we have some complex regular expression logic. We specify some Kleene closures to access data within paragraph tags.
Tip The star character, meaning zero or more repeats, is a Kleene closure and it matches whitespace and the inner value for us here.
Dim html as String = "<html><title>...</title><body><p>Result.</p></body></html>"
Function GetFirstParagraph(value as String)
' Use regular expression to match a paragraph.
Dim match as Match = Regex.Match(value, "<p>\s*(.+?)\s*</p>")
Some notes. When using the Groups property on a Match result from Regex.Match, it is important to access element 1 for the first group. The collection is one-based, not zero-based.
Summary. Accessing inner text values from within HTML strings can be difficult. And regular expressions are not the best solution in all cases, but they can work on simple HTML pages.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.