Hi,
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.
Using

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

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

Thanks
Newbie Simon

Recommended Answers

All 3 Replies

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)
            MsgBox(m.Value)
        Next

    End Sub

End Class

The output will be

    32
    355
    3456

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".

Great
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.