Problem parsing strings using wildcards

Posted on 2006-06-01
Last Modified: 2010-04-17
Is it possible to grab an excerpt of a string, via wildcards, and put it into another string? I've been trying to set a string equal to another string using the LIKE operator with some wilcards, but VB.NET doesn't seem to like that.

For example, I have a streamreader that is grabbing lines of text from a log file and dropping the lines into a string, one at a time, for parsing. I would like to search that string for OBJ:{????????-????-????-????-????????????} (with the ?s as wildcards) and then place that excerpt into a new string. Is this possible?

I've been reading about substrings, but it seems like all of those assume that you will have the length, or starting or ending characters.

Question by:Mister_Tog
    LVL 14

    Accepted Solution

    Regular Expressions are the way to go for this sort of string parsing.  Unfortunately I am not all that familiar with .NET, so my example will actually be in VB6 / VBScript code instead of using whatever RegEx implementation is available in .NET:

    Dim objRE, colMatches, objMatch
    Dim strString, strExcerpt

    Set objRE = CreateObject("VBScript.RegExp")

    objRE.Pattern = "OBJ:\{.{8}-.{4}-.{4}-.{4}-.{12}\}"
    objRE.IgnoreCase = True

    strString = "Blah Blah Blah OBJ:{12345678-1234-1234-1234-123456789012} Blah Blah Blah"

    Set colMatches = objRE.Execute(strString)

    For Each objMatch In colMatches
      strExcerpt = objMatch.Value
    Next objMatch

    MsgBox strExcerpt
    LVL 1

    Assisted Solution

    I had done the same thing but I knew that the values were either a-z, A-Z, or 0-9. So I searched the string by an ANSI range of values. So my var, your ?, was equal to those ranges. If they were, I built a string from them.

    ANSI A-Z = Chr(065) - Chr(090)
    ANSI a-z = Chr(097) - Chr(122)
    ANSI 0-9 = Chr(048) - Chr(057)
    LVL 2

    Assisted Solution

    You only need to paste these codes and it should work fine

    Dim pattern As String = "OBJ:{(.{8})-(.{4})-(.{4})-(.{4})-(.{12})}"
    Dim mytext As String = "ABC OBJ:{12345678-9012-3456-7890-123456789012}"

    Dim c As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(mytext, pattern)

    If c.Count > 0 Then

          Dim TargetReplaceText As String = c(0).Value.Substring(5)

          Dim separator(1) As Char
          separator(0) = "-"

          Dim items() As String = TargetReplaceText.Split(separator)

         ' Replace your own text below
         items(0) = "abcdefgh"
         items(1) = "ijkl"
         items(2) = "mnop"
         items(3) = "qrst"
         items(4) = "uvwxyzabcdef"

          Dim newvalue As String = "OBJ:{" + String.Join("-", items) + "}"

    End If
    LVL 8

    Assisted Solution

           'assuming this is where you put your lines of text
            Dim arrTxt As ArrayList = New ArrayList

            arrTxt.Add("some text OBJ:{1AAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE}")
            arrTxt.Add("OBJ:{2AAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE} some text")
            arrTxt.Add("some text OBJ:{3AAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE} some text")
            arrTxt.Add("this will not be matched OBJ:{4AAAAAAA-BBBB-CCCC-DDD-EEEEEEEEEEEE} some text")

            Dim re As System.Text.RegularExpressions.Regex

            re = New System.Text.RegularExpressions.Regex(".*?(OBJ:\{.{8}-.{4}-.{4}-.{4}-.{12}\}).*", System.Text.RegularExpressions.RegexOptions.IgnoreCase)

            Dim match As System.Text.RegularExpressions.Match

            For Each line As String In arrTxt
                match = re.Match(line)
                If match.Success Then
                End If

    LVL 1

    Author Comment

    Sorry for the delay in getting back to this thread. I ended up using a combination of the suggestions. Unfortunately, I don't access to the code that I ended up using handy.

    Thanks again for your help!

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    wefewf 2 28
    array6 challenfge 6 49
    Installed softare without GUID 6 30
    Microsoft C++ code failing in executable that worked 9 24
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    This is about my first experience with programming Arduino.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now