Need a function to take a string, and return numbers in that string

The function will be passed

ie

thepath + picnum1256.jpg
 and so on

needs to return:
1256
help!

pdidowAsked:
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.

tomvergoteCommented:
what language do you want that in?
0
pdidowAuthor Commented:
VB.NET
0
pratap_rCommented:
simple... use regex

        Dim r As New System.Text.RegularExpressions.Regex("[^0-9]*") '<-- one liner
        MsgBox(r.Replace("thepath + picnum1256.jpg", "")) 'example 1.will display 1256
        MsgBox(r.Replace("Hello..123,4+5~6&7*8(9)0", "")) 'example 2. will display 1234567890


Have Fun!
Pratap
0
pratap_rCommented:
mm just to explain what i did

        Dim r As New System.Text.RegularExpressions.Regex("[^0-9]*") '<-- one liner

declaring a new object of type Regex with [^0-9]* as pattern. Meaning match all characters which are not in the range 0-9, effectively any non numerals.

        MsgBox(r.Replace("thepath + picnum1256.jpg", "")) 'example 1.will display 1256

the r.Replace i used here takes two parameters, the string to search for and the string to replace the match with. The regex engine applies the pattern given to it in the constructor and for each match it substitutes the replacement string (the second parameter).

        MsgBox(r.Replace("Hello..123,4+5~6&7*8(9)0", "")) 'example 2. will display 1234567890

This line is just to show how the filtering works.

putting this in a function....

    Function GetNumber(ByVal s As String) As String
        Dim r As New System.Text.RegularExpressions.Regex("[^0-9]*")
        GetNumber = r.Replace(s, "")
    End Function

and your calls will be
        MsgBox(GetNumber("Hello..123,4+5~6&7*8(9)0")) 'will display 1234567890
        MsgBox(GetNumber("thepath + picnum1256.jpg")) 'will display 1256


Have Fun!
Pratap
0

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
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
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.