Results 1 to 11 of 11

Thread: Extract values from a string

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Resolved Extract values from a string

    Hi to all:

    I have a string Like this:

    Msg = "ffff error msg 0081456378390 00827894546738"

    I would Like to extract from this string all the values that start by 0081 and 0082.
    With way i can do this?

    Thanks
    Last edited by sacramento; May 24th, 2013 at 08:04 AM. Reason: RESOLVED

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Extract values from a string

    you can do that with regex:

    Code:
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Msg As String = "ffff error msg 0081456378390 00827894546738"
            For Each m As Match In New Regex("008(1|2)\d+").Matches(Msg)
                MsgBox(m.Value)
            Next
        End Sub
    
    End Class

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

    Re: Extract values from a string

    Split the string on spaces. If the values always occur in the same positions then use the index in the resulting array to get them. Otherwise loop through the array for those that start with the required pattern.
    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!

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: Extract values from a string

    Hi .Paul:

    Your code work perfectly, but i think it cacth all "0081" and "0082" string correct? In this case i just cacth always the last message, i this case "0082" because is the last string...It's correct?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Extract values from a string

    yes it catches all "0081" and "0082" values, as you asked for...

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: Extract values from a string

    Ok...but i can't have both values (0081 and 0082 strange no? do you had test?) and the first "0081" don't return all message if i have a message like this:
    00812563695869d085f526
    The code return: "00812563695869"
    do you know why?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Extract values from a string

    Code:
    New Regex("008(1|2)\w+")

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: Extract values from a string

    PERFECT...

    Thanks a lot for your help...

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: Extract values from a string

    HI:

    Sory return to the post again but could i Cacth more than one value:

    We have this:
    For Each m As Match In New Regex("008(1|2)\w+").Matches(msg)

    for catch 0081 and 0082...If i have in the message 0051 and 0070 could i put in the line more this two conditions?
    I had try a few thinks but without exit!

    Thanks

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Extract values from a string

    Code:
    New Regex("00(81|82|51|70)\w+")

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: Extract values from a string

    Fantastic...thanks a lot!

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