How to: Identify URIs in a String in Visual Basic
This example demonstrates how to use a simple regular expression to identify a uniform resource identifier (URI) in a string. To reduce the number of false positives (text mistakenly identified as a URI), a specific URI format is assumed. This means there will be some false negatives, where valid URIs are not identified.
Example
A specific form of URI can be matched with the regular expression ([^=":\s]+:)?//[^\s"]+
, which means:
Optional appearance of:
Set of one or more characters that are not
=
,"
,:
, or a space character, followed byThe
:
character, followed by
The string
//
, followed bySet of one or more characters that are not quotation marks or space characters.
The Regex object is initialized with the regular expression.
The Regex object's Matches method returns a MatchCollection object that contains information about all the parts of the input string that the regular expression matches.
''' <summary>Identifies URIs in text.</summary>
''' <param name="text">Text to parse.</param>
''' <remarks>Displays each URI in the input text.</remarks>
Sub IdentifyURIs(ByVal text As String)
Dim uriRegex As New Regex("([^="":\s]+:)?//[^\s""]+")
Dim output As String = ""
For Each m As Match In uriRegex.Matches(text)
output &= m.Value & vbCrLf
Next
MsgBox(output)
End Sub
This example requires that you use the Imports statement to import the System.Text.RegularExpressions namespace. For more information, see Imports Statement.
See Also
Tasks
How to: Identify Hyperlinks in an HTML String in Visual Basic
How to: Strip Invalid Characters from a String