0

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

Edited by gbhs

2
Contributors
3
Replies
19
Views
2 Years
Discussion Span
Last Post by gbhs
0

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?

0

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

Edited by Reverend Jim

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.