Solved

Problem finding pattern matching and parsing it.

Posted on 2000-03-28
3
188 Views
Last Modified: 2010-05-02
I have a string "EXEC (@SQL1 + @SQL2 + @SQL3)". My task is to find all substrings starting with '@'. Having to find such a string, I need to write that string to the database. For example, write '@SQL1', '@SQL2', and '@SQL3' to the database. The writing part to the database is not a problem. The hard part is finding the matching pattern.

Hope someone could help me with that.
0
Comment
Question by:ervinloh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 28

Expert Comment

by:Ark
ID: 2666071
Hi
Private Sub Command1_Click()
Dim nStart As Integer, nEnd As Integer, i As Integer
Dim s As String, sqlArr() As String
s = "EXEC (@SQL1 + @SQL2 + @SQL3)"
nEnd = 1
Do
  i = i + 1
  nStart = InStr(nEnd, s, "@") + 1
  If nStart = 1 Then Exit Do
  nEnd = InStr(nStart, s, "+") - 1
  If nEnd < 0 Then nEnd = Len(s)
  ReDim Preserve sqlArr(i)
  sqlArr(i) = Mid$(s, nStart, nEnd - nStart)
Loop
End Sub
Cheers
0
 
LVL 4

Expert Comment

by:gcs001
ID: 2667249
Private Sub Command1_Click()
Dim s As String
Dim outstr As String
Dim nextpos As Long

s = "EXEC (@SQL1 + @SQL2 + @SQL3)"
' Remove everything up til first @
s = Mid$(s, InStr(1, s, "@"))
' Remove last ')' and append a ' +'
s = Mid$(s, 1, InStr(1, s, ")") - 1) & " +"

nextpos = InStr(nextpos + 1, s, "@")
Do Until nextpos = 0
    outstr = Mid$(s, nextpos, InStr(nextpos + 1, s, " +") - nextpos)
    'insert outstr into table
    nextpos = InStr(nextpos + 1, s, "@")
Loop
End Sub
0
 
LVL 9

Accepted Solution

by:
cymbolic earned 100 total points
ID: 2670564
Here's a little token parser I've developed and used for years:

Public Function Toke$(Inst$, Sep$)
'takes off first substring in inst$, shortening inst$ each time. Destructive to Inst$.
Dim i As Integer
i = InStr(Inst$, Sep$)
If i = 0 Then
  Toke$ = Inst$
  Inst$ = ""
Else
  Toke$ = Left$(Inst$, i - 1)
  Inst$ = Mid$(Inst$, i + Len(Sep$))
End If
End Function

Using this function you would parse your sample data like this:

ParseWrite "EXEC (@SQL1 + @SQL2 + @SQL3)"

sub ParseWrite(istr$)
dim x$,y$
y$=istr$  'because toke$ is destructive
x$=toke$(y$,"(") 'istr gets "EXEC (" peeled off
y$=toke$(y$,")") 'now we have just @SQL1+@SQL2+@SQL3 in y$
while len(y$)>0
 x$=toke$(y$,"+")
 'write x$ value to database
wend

end sub
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
A macro to Count the number of rows across all worksheets 3 150
Run code from text file in vb 1 87
Spell Check in VB6 13 137
adding "ungroup sheets" to existing vbs code 5 54
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 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