Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,181 questions

56,073 answers

573 users

How to extract all floating-point numbers from a string of words in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Text.RegularExpressions

Module Program

    ' Compiled Regex pattern matching standalone numbers with explicit decimal points.
    ' \b defines word boundaries; \d+ matches whole digits before and after the dot.
    Private ReadOnly FloatPattern As New Regex("\b\d+\.\d+\b", RegexOptions.Compiled)

    ''' <summary>
    ''' Extracts all floating-point numbers containing explicit decimal points from an input string.
    ''' </summary>
    ''' <param name="input">The source string containing text and numerical tokens.</param>
    ''' <returns>A list of parsed double precision floating-point numbers.</returns>
    Public Function ExtractFloats(input As String) As List(Of Double)
        Dim results As New List(Of Double)()

        If String.IsNullOrWhiteSpace(input) Then
            Return results
        End If

        ' Find all regex matches matching the floating-point pattern
        Dim matches As MatchCollection = FloatPattern.Matches(input)

        For Each match As Match In matches
            Dim parsedValue As Double
            
            ' CultureInfo.InvariantCulture ensures the decimal point (.) is parsed correctly
            ' regardless of the local system's regional settings (e.g., regions using commas).
            If Double.TryParse(match.Value, NumberStyles.Float, CultureInfo.InvariantCulture, parsedValue) Then
                results.Add(parsedValue)
            End If
        Next

        Return results
    End Function

    Sub Main()
        ' Example input string
        Dim s As String = "c/c++ c# go 893725.1045 java python 3.14 php 0.0076 javascript"

        ' Call extraction function
        Dim floatNumbers As List(Of Double) = ExtractFloats(s)

        ' Display extracted numbers using InvariantCulture to print standard dots
        Console.WriteLine("Extracted floating-point numbers:")
        For Each number As Double In floatNumbers
            Console.WriteLine(number.ToString(CultureInfo.InvariantCulture))
        Next
    End Sub

End Module



' run:
' 
' Extracted floating-point numbers:
' 893725.1045
' 3.14
' 0.0076
'

 



answered 11 hours ago by avibootz

Related questions

...