[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem parsing strings using wildcards

Posted on 2006-06-01
6
Medium Priority
?
385 Views
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.

Thanks!
0
Comment
Question by:Mister_Tog
5 Comments
 
LVL 14

Accepted Solution

by:
dlwyatt82 earned 800 total points
ID: 16809153
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
0
 
LVL 1

Assisted Solution

by:edfreels
edfreels earned 400 total points
ID: 16809825
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)
0
 
LVL 2

Assisted Solution

by:akumanova
akumanova earned 400 total points
ID: 16812887
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) + "}"

      Console.WriteLine(newvalue)
End If
0
 
LVL 8

Assisted Solution

by:harris_c
harris_c earned 400 total points
ID: 16824282
       '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
                MessageBox.Show(match.Groups(1).ToString)
            End If
        Next

hec",)
0
 
LVL 1

Author Comment

by:Mister_Tog
ID: 17066302
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!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Introduction to Processes
Loops Section Overview

872 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