[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Problem finding pattern matching and parsing it.

Posted on 2000-03-28
3
Medium Priority
?
194 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 300 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

656 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