1

I have the following SQL Server code which I would like to do in C# code. The logic is - if the pattern '%SELECT %FROM% is found in a string called 'x', then I need to return. The '%' stands for 0 or more characters.

The part I am not getting is how to translate the first line of code into C#?

 IF PATINDEX('%SELECT %FROM%',  @x ) > 0 
  BEGIN
     RETURN;
  END
1
  • You can post some sample code please? Commented Sep 30, 2013 at 21:27

1 Answer 1

3

Take a look at regular expresions for C# and the match function..

The pattern would be something like

.*SELECT .*FROM.*

where .* stands for anything other then a new line.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks your pattern is perfect. One needs to always replace % in SQL Server pattern with .* in C# Regex. That rule is always true it seems.
The one gotcha is that .* won't match new lines by default. If you want .* to match new lines then you need to use RegexOptions.Singleline.
Ok. I will try that out.
Vulcranos - You are a genius. Your advice is worth a million points. Thanks for that excellent tip on RegexOptions.Singleline

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.