Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Am writing an app in VB.NET and I want to replace occurrences of ? with a sequential of @param1, @param2, @paramn
eg
SQL
INSERT INTO X (a,b,c) values (?,?,?)
should become
SQL
INSERT INTO X (a,b,c) values (@param1,@param2,@param3)


Can I achieve this with regex?
Posted
Comments
Roliking 12-Apr-12 1:01am    
do you want to do this is sql or in vb.net code?

Use :
(?<params>\(\s*\?.*)+

Which will give you the (...) portion and then just count the number of ? chars and replace with @paramN where N is an iterator.
 
Share this answer
 
v2
Comments
Espen Harlinn 14-Apr-12 13:30pm    
Good idea
VJ Reddy 14-Apr-12 20:17pm    
Good idea. 5!
RaisKazi 14-Apr-12 22:16pm    
My 5.
One option is to split the original string at the location of ? and reconstruct using @param as shown below
VB
Sub Main
    Dim statement As String = "INSERT INTO X (a,b,c) values (?,?,?)"
    Dim statements As String() = statement.Split("?"C)

    'Construct a stringbuilder
    Dim sb As New StringBuilder()
    For i As Integer = 0 To statements.Length - 2
        sb.AppendFormat("{0}@param{1}", statements(i), i + 1)
    Next
    sb.Append(statements(statements.Length - 1))

    Dim modifiedStatement As String = sb.ToString()
    Console.WriteLine(modifiedStatement)

End Sub
'Output
'INSERT INTO X (a,b,c) values (@param1,@param2,@param3)
 
Share this answer
 
v2
Comments
Mike Chibaka 13-Apr-12 0:23am    
Than you a million times VJ. your snippet worked wonders :)
VJ Reddy 13-Apr-12 1:11am    
You're welcome and thank you for the appreciation.
Espen Harlinn 14-Apr-12 13:30pm    
5'ed!
VJ Reddy 14-Apr-12 20:10pm    
Thank you, Espen Harlinn.
RaisKazi 14-Apr-12 22:16pm    
My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900