How to extract a float from a string in VB.NET

1 Answer

0 votes
Imports System
Imports System.Text.RegularExpressions

Class Program
    Public Shared Sub Main()
        Dim str As String = "The price is 148.95 dollars"

        Dim floatRegex As Regex = New Regex("[-+]?\d*\.\d+|\d+")
        Dim match As Match = floatRegex.Match(str)

        If match.Success Then
            Dim number As Single = Single.Parse(match.Value)
            Console.WriteLine("Extracted float: " & number)
        End If
    End Sub
End Class



' run:
' 
' Extracted float: 148.95
'

 



answered Jul 28, 2025 by avibootz
...