Visual Basic Code Snippet - Extract Numeric values

Visual Basic Code Snippet - Extract Numeric values

Visual Basic Code Snippet - Extract Numeric values

(VB) Visual Basic code snippet to extracts all the numeric values from a string. ExtractNumbers returns string array of numeric values successful matches by iteratively applying a regular expression pattern to the input string.

Bookmark:

Visual Basic Code Snippet - Extract Numeric values

This .Net Visual Basic code snippet extracts all the numeric values from a string. Data mining for numeric values done by set of successful matches found by iteratively applying a regular expression pattern to the input string.

Public Function ExtractNumbers(ByVal str As String) As String()
    ' Find matches.
    Dim matches As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(str, "(\d+\.?\d*|\.\d+)")

    Dim MatchList(matches.Count - 1) As String

    ' Report on each match.
    Dim c As Integer = 0
    For Each match As System.Text.RegularExpressions.Match In matches
        MatchList(c) = match.ToString()
        c += 1
    Next match

    Return MatchList
End Function


VB Keywords Used:

  • Regex
  • Match
  • MatchCollection
  • Regex.Matches

Code Snippet Information:

  • Applies To: .Net, VB, CLI, Data Mining, Numeric Value Extract, Regular Expression
  • Programming Language : Visual Basic (VB)

External Resources:

Leave a comment