I have a string like 32/355ab//3456 from which I need to extract
the three separate numbers 32, 355 and 3456.
The string pattern may vary.
I can use Split by (say) / to separate strings.
How can I extract groups of numbers from a 'mixed' string like
above. The string length and number of non numeric chars may vary.

Dim numbers = Regex.Replace("32/355ab//3456", "\D", "")

does not give me separate numbers
(It replaces non digits and retains 323553456 as one number).

Newbie Simon

Edited 1 Year Ago by gbhs

The following seems to work on button click

        Dim myString As String = TextBox1.Text.Trim
        Dim splitter As String = "\D+"
        Dim result() As String = Regex.Split(myString, splitter)
        Label1.Text = result(0)
        Label2.Text = result(1)
        Label3.Text = result(2)

for 32/355ab//3456 giving 32, 355 and 3456 in respective labels.

But does not go for pp32/355ab//3456. Why so?

You can use regular expressions. Here's your example

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim rx As New Regex("\d+")

        Dim s As String = "32/355ab//3456"

        For Each m As Match In rx.Matches(s)

    End Sub

End Class

The output will be


The expression \d+ matches the longest possible string of digits. That means if the string contains "32", it will match "32" rather than "3", then "2".

Edited 1 Year Ago by Reverend Jim

This article has been dead for over six months. Start a new discussion instead.