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:

  1. Optional appearance of:

    1. Set of one or more characters that are not =, ", :, or a space character, followed by

    2. The : character, followed by

  2. The string //, followed by

  3. Set 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 (.NET Namespace and Type).

See Also

Tasks

How to: Identify Hyperlinks in an HTML String in Visual Basic

How to: Strip Invalid Characters from a String

Other Resources

Parsing Strings in Visual Basic