2

I'm trying to read scripts from various .sql files into a string and execute them in SQL server using c#. I want to remove the terms USE, GO etc. I succeeded removing one word at a time but cannot find the pattern to search for two or more words at the same time.

so far,

string pattern =  @"\bUSE*\b";
string script = Regex.Replace(script, pattern, "");

How to add other search terms to the pattern? i tried msdn docs and previous questions but cannot find what i need. Any help is greatly appreciated.

2 Answers 2

3

You can use pipe in your keywords.

string pattern =  @"\b(USE|GO|MORE|FEW_MORE|AND_SO_ON)\b";
Sign up to request clarification or add additional context in comments.

9 Comments

Seriously, by 7 seconds?? :-) - And yours is neater!
This works thanks. What if I need to search for something like this [string] ? I tried @"\b(USE|GO|[table1])\b"; but it doesn't work. thanks again
@kcube you need to escape the square brackets with backslash
You have to escape the [ and ] characters. Like this way: \[table1\]
@"\b(USE|[Northwind])\b"; It is still showing [Northwind] in the script :\
|
0

I'm a little rusty, but have you tried

string pattern = @"\bUSE*\b|\bGO*\b"

??

1 Comment

This removes only USE :\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.