|
List Info
Thread: Break apart a SQL stored procedure stirng
|
|
| Break apart a SQL stored procedure
stirng |

|
2007-12-20 11:24:19 |
|
We're building an ASP.Net app (VB) where one part of the process needs to take a command string and break it apart into its constituent pieces: stored procedure name, and each argument name with its value (may have one or more args, or none). A string may look like any of the following:
EXEC stpMyProcN ame StartDate='12/1/2007', EndDate=39;12/31/2007'
EXEC stpMyProcName FullName='John Q. Smith'
etc.
The expression we have come up with so far sort of breaks it apart, but not quite the way we want:
(?!<=EXECs)(.*s)(?<Parms> w+='w+',*)*
This gives two groups:
1: [stpMyProcName]
Parms: [everything after the first or nothing if no arguments]
What we would like to have is X number of groups where the first group is the Procedure Name and the remaining X number of groups be each Argument, like such (using the first example above):
Proc: [stpMyProcName]
StartDate: 12/1/2007
EndDate: 12/31/2007
or if there are no Arguments, just give one group, that of the Procedure Name.
Is this possible? If so, could someone lend a hand with the expression? Or, if what I have said is not clear, please ask and I will try to explain it another way.
Thanks!
-- Andrew
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Regex" group. To post to this group, send email to regex googlegroups.com To unsubscribe from this group, send email to regex-unsubscribe googlegroups.com For more options, visit this group at http://groups.google.com/group/regex -~----------~----~----~----~------~----~------~--~---
|
| Re: Break apart a SQL stored procedure
stirng |

|
2007-12-20 20:57:17 |
Andrew,
I think the regular expression you want is this one:
EXECs+(?<Proc>w+)(s+( (?<Param>w+)=(?<Value>[^,]+),?)*)?
One part that might need tweaking is the Value group. I
made it match
anything that isn't a comma, since commas are used to
separate
parameters, but if a parameter value can contain a comma,
you might
need to adjust that part of the expression.
Below is the code I used to test this expression.
Dim procRegex As Regex
procRegex = New Regex( _
"EXECs+(?<Proc>w+)(s+( (?<Param>w+)=(?<Value>[^,]+),?)*)?"
)
Dim procMatch As Match
Dim testStrings() As String
testStrings = New String() { _
"EXEC stpMyProcName StartDate='12/1/2007', EndDate='12/31/2007'", _
"EXEC stpMyProcName", _
"EXEC stpMyProcName FullName='John Q.
Smith'", _
"EXEC stpMyProcName ID=14, Age=21, Phone=NULL, Date='12/31/2007'"}
For Each testString As String In testStrings
procMatch = procRegex.Match(testString)
If (procMatch.Success) Then
Console.WriteLine("*************************")
Console.WriteLine(String.Format("Procedure:{0,15}"
, _
procMatch.Groups.Item("Proc").Value))
Dim paramGroup =
procMatch.Groups.Item("Param")
Dim valueGroup =
procMatch.Groups.Item("Value")
If (paramGroup.Success) Then
Console.WriteLine()
Console.WriteLine("Parameter
Value")
Console.WriteLine("-------------------------")
For i As Integer = 0 To
paramGroup.Captures.Count - 1
Console.WriteLine( _
String.Format("{0,-10}{1,15}",
_
paramGroup.Captures.Item(i).Value, _
valueGroup.Captures.Item(i).Value))
Next
End If
Console.WriteLine("*************************")
Console.WriteLine()
End If
Next
I hope this helps.
Jeff
On Dec 21, 12:24 am, "Andrew Raastad"
<andrew... gmail.com> wrote:
> We're building an ASP.Net app (VB) where one part of
the process needs to
> take a command string and break it apart into its
constituent pieces: stored
> procedure name, and each argument name with its value
(may have one or more
> args, or none). A string may look like any of the
following:
>
> EXEC stpMyProcName StartDate='12/1/2007', EndDate='12/31/2007'
> EXEC stpMyProcName
> EXEC stpMyProcName FullName='John Q. Smith'
> EXEC stpMyProcName ID=14, Age=21, Phone=NULL, Date='12/31/2007'
> etc.
>
> The expression we have come up with so far sort of
breaks it apart, but not
> quite the way we want:
> (?!<=EXECs)(.*s)(?<Parms> w+='w+',*)*
>
> This gives two groups:
> 1: [stpMyProcName]
> Parms: [everything after the first or
nothing if no arguments]
>
> What we would like to have is X number of groups where
the first group is
> the Procedure Name and the remaining X number of groups
be each Argument,
> like such (using the first example above):
> Proc: [stpMyProcName]
> StartDate: 12/1/2007
> EndDate: 12/31/2007
>
> or if there are no Arguments, just give one group, that
of the Procedure
> Name.
>
> Is this possible? If so, could someone lend a hand
with the expression?
> Or, if what I have said is not clear, please ask and I
will try to explain
> it another way.
>
> Thanks!
>
> -- Andrew
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regex googlegroups.com
To unsubscribe from this group, send email to
regex-unsubscribe googlegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Break apart a SQL stored procedure
stirng |

|
2007-12-21 09:12:05 |
|
Thank you very much Jeff, we'll bang this guy around and see what we can do with it. Always appreciate the help!! 
-- Andrew
On 12/20/07, Jeff < jeff.hillman gmail.com">jeff.hillman gmail.com> wrote:
Andrew,
I think the regular expression you want is this one:
EXECs+(?<Proc>w+)(s+( (?<Param>w+)=(?<Value>[^,]+),?)*)?
One part that might need tweaking is the Value group. I made it match anything that isn't a comma, since commas are used to separate parameters, but if a parameter value can contain a comma, you might
need to adjust that part of the expression.
Below is the code I used to test this expression.
Dim procRegex As Regex procRegex = New Regex( _ "EXECs+(?<Proc>w+)(s+( (?<Param>w+)=(?<Value>[^,]+),?)*)?")
Dim procMatch As Match
Dim testStrings() As String testStrings = New String() { _ "EXEC stpMyProcName StartDate='12/1/2007', EndDate=39;12/31/2007'", _ "EXEC stpMyProcName", _
"EXEC stpMyProcName FullName='John Q. Smith'", _ "EXEC stpMyProcName ID=14, Age=21, Phone=NULL, Date='12/31/2007'"}
For Each testString As String In testStrings procMatch =
procRegex.Match(testString)
If (procMatch.Success) Then Console.WriteLine("*************************") Console.WriteLine(String.Format("Procedure:{0,15}", _
procMatch.Groups.Item("Proc").Value))
Dim paramGroup = procMatch.Groups.Item("Param") Dim valueGroup = procMatch.Groups.Item("Value")
If (paramGroup.Success
) Then Console.WriteLine() Console.WriteLine("Parameter Value") Console.WriteLine("-------------------------")
For i As Integer = 0 To
paramGroup.Captures.Count - 1 Console.WriteLine( _ String.Format("{0,-10}{1,15}", _ paramGroup.Captures.Item(i).Value, _ valueGroup.Captures.Item
(i).Value)) Next End If
Console.WriteLine("*************************") Console.WriteLine() End If Next
I hope this helps.
Jeff
On Dec 21, 12:24 am, "Andrew Raastad" <
andrew... gmail.com">andrew... gmail.com> wrote: > We're building an ASP.Net app (VB) where one part of the process needs to > take a command string and break it apart into its constituent pieces: stored
> procedure name, and each argument name with its value (may have one or more > args, or none). A string may look like any of the following: > > EXEC stpMyProcName StartDate='12/1/2007', EndDate=39;12/31/2007'
> EXEC stpMyProcName > EXEC stpMyProcName FullName='John Q. Smith' > EXEC stpMyProcName ID=14, Age=21, Phone=NULL, Date='12/31/2007' > etc. > > The expression we have come up with so far sort of breaks it apart, but not
> quite the way we want: > (?!<=EXECs)(.*s)(?<Parms&g
| |