parsing a string variable and get the numbers

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"
LVL 1
maqskywalkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
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-
maqskywalkerAuthor 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?
it_saigeDeveloperCommented:
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-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Bill PrewIT / Software Engineering ConsultantCommented:
To get negative via RegEx try:

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

Open in new window


»bp
maqskywalkerAuthor Commented:
thanks. Both great examples!
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome, glad that was useful.


»bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.