parsing a string variable and get the numbers

maqskywalker
maqskywalker used Ask the Experts™
on
I'm using visual basic .net in a aspx web forms application

If I have a string variable that looks like this that contains numbers and characters.

How do I save only the numbers in a different variable called Intvariable1 ?

Testvariable1 = "123|YYY"

sometimes the values in TestVariable1 will be this "123|YYY"  or this "12|YYY"  or this "3|YYY"
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
If that is your value set, then I would split on the pipe and TryParse the splits as a numeric type; e.g. -
Module Module1
    Sub Main()
        Dim values = New String() {"123|YYY", "12|YYY", "3|YYY"}
        For Each value In values
            Dim result As Integer
            Dim selects = (From split In value.Split(New String() {"|"c}, StringSplitOptions.RemoveEmptyEntries)
                           Where Integer.TryParse(split, result)
                           Select split)
            For Each selected In selects
                Console.WriteLine($"{value} contains {selected}")
            Next
        Next
        Console.ReadLine()
    End Sub
End Module

Open in new window

Which produces the following output -Capture.PNG-saige-

Author

Commented:
On this link
https://stackoverflow.com/questions/13356018/get-only-numbers-from-string

I saw this example:

resultString = Regex.Match(subjectString, @"\d+").Value;


if subjectstring = "28|NN"

then it returns 28

But it only works for positive numbers.

Because if subjectstring = "-1|NN"

then it returns 1

How do fix regex example to also work with negative numbers?
Commented:
My example works with both positive and negative numbers; e.g. -
Module Module1
    Sub Main()
        Dim values = New String() {"123|YYY", "12|YYY", "3|YYY", "-3|YYY", "-12|YYY", "-123|YYY"}
        For Each value In values
            Dim result As Integer
            Dim selects = (From split In value.Split(New String() {"|"c}, StringSplitOptions.RemoveEmptyEntries)
                           Where Integer.TryParse(split, result)
                           Select split)
            For Each selected In selects
                Console.WriteLine($"{value} contains {selected}")
            Next
        Next
        Console.ReadLine()
    End Sub
End Module

Open in new window

Produces the following output -Capture.PNG-saige-
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Bill PrewTest your restores, not your backups...
Top Expert 2016
Commented:
To get negative via RegEx try:

resultString = Regex.Match(subjectString, @"[\d-]+").Value;

Open in new window


»bp

Author

Commented:
thanks. Both great examples!
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Welcome, glad that was useful.


»bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial