Results 1 to 7 of 7

Thread: [RESOLVED] Getting the number from the string

  1. #1

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Resolved [RESOLVED] Getting the number from the string

    Hi Guys.

    I am looking for a little advice on how I can do something. Please give me your suggestions.

    Pretend I have some string values like this:

    Document1_789123456.docx

    3rd_Notice_123456789_Final.pdf

    001002003_SallySample_Upload.xls


    I know I can use RegEx to find out if the strings have a nine digit employee number in the string. But I also want to get the 9 digit employee number from the file.

    How would you recommend I get the 9 digit employee number? It’s important that I get all 9 digits. If the number if 8 digits long, then it’s not the employee number.

    Since RegEx only tells me if the value exists (and not where in the string it exists), I was thinking maybe I could loop through the string and count how many numbers in a row I find.

    What do you think? Is there a better way?
    ~Peter


  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Getting the number from the string

    Since RegEx only tells me if the value exists (and not where in the string it exists)
    Er ... I suggest you revise RegEx. The whole point it is to extract sections of text.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Getting the number from the string

    And if you don;t want to use regex you could drop the extension and split the filename on the underscore then loop through the resulting array to locate the element where the 9 digit number occurs.

  4. #4

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Re: Getting the number from the string

    I went back and looked at all my RegEx examples, and none of them have anything about extracting anything from the string - they're all comparison checks, like if an email address is valid. So i looked further, and i see that Match has an Index property - the location of where it found the match. Thanks dunfiddlin!

    Any ideas on how to create the Regular Expression to locate the nine digit number? I've always had trouble creating those expressions. How do you make the [0-9] span 9 numbers in a row?
    ~Peter


  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Getting the number from the string

    Quote Originally Posted by MrGTI View Post
    So i looked further, and i see that Match has an Index property - the location of where it found the match.
    You're making this harder than it needs to be. You don't extract the index of the match and then go back to the original string. Your regex should include a Capture Group that is returned as part of the regex result. You can simply get these values out of each Match. You do this with parentheses.

    Quote Originally Posted by MrGTI View Post
    Any ideas on how to create the Regular Expression to locate the nine digit number? I've always had trouble creating those expressions. How do you make the [0-9] span 9 numbers in a row?
    You specify that you want to repeat the element 9 times with the appropriate command in the regex. Use braces for this.

    Do you have a Regex reference? Sounds like you could do with one.

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Getting the number from the string

    Have a look at this site, its an excellent place for testing out how regex works: http://gskinner.com/RegExr/

    Just paste in your example filesnames above, and a regex sample at the top that looks like this:

    ([0-9]{9})

    Which very simply says group ready for extract (brackets) the character [0-9] exactly {9} times.

    There is an example here, http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx that you can look at to see how you extract the captures (groups) of the numbers, the example is overkill for what you need as you expect only 1 extract.
    Last edited by Grimfort; Jan 17th, 2013 at 10:21 AM.

  7. #7

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Re: Getting the number from the string

    Thanks for that shove in the right direction Grimfort. The MSDN page proved usefull. I was able to build a function the get the Employee number in just a few simple lines of code.

    Code:
        Private Function GetEmployeeNumber(ByVal sInputText As String) As String
            Dim sReturn As String = ""
            Try
                Dim r As Regex = New Regex("([0-9]{9})", RegexOptions.IgnoreCase)
                Dim m As Match = r.Match(sInputText)
                If m.Success = True Then sReturn = m.Value
            Catch Exp As Exception
                sReturn = ""
            End Try
            Return sReturn
        End Function
    Thanks guys!
    ~Peter


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width