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
Sunil
  • 20,653
  • 28
  • 112
  • 197

1 Answers1

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.

Vulcronos
  • 3,428
  • 3
  • 16
  • 24
  • 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. – Sunil Sep 30 '13 at 21:57
  • 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. – Vulcronos Sep 30 '13 at 22:01
  • Ok. I will try that out. – Sunil Oct 01 '13 at 05:41
  • Vulcranos - You are a genius. Your advice is worth a million points. Thanks for that excellent tip on RegexOptions.Singleline – Sunil Oct 02 '13 at 01:28