-1

I have regex like this:

(?i)^(?!.*\bWITH\b).*\(\s*.*\s*\b(INDEX|FASTFIRSTROW|HOLDLOCK|SERIALIZABLE|REPEATABLEREAD|READCOMMITTED|READUNCOMMITTED|ROWLOCK|PAGLOCK|TABLOCK|TABLOCKX|NOLOCK|UPDLOCK|XLOCK|READPAST)\b\s*.*\s*\)

It return true in http://regexstorm.net.

But when i run in C#, it always return false.

String input to text:

INNER JOIN t_hat_meisaimidasi AS MM (READCOMMITTED, NOLOCK) WHERE ( AND hat_kanri_no = ? 

Can someone explain me why?

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
Ngan Mai
  • 77
  • 7
  • 1
    Thinking sideways for a moment: the [DacFx package](https://www.nuget.org/packages/Microsoft.SqlServer.DacFx.x64/) includes a fully featured T-SQL parser (`Microsoft.SqlServer.TransactSql.ScriptDom.TSqlParser`). This may be more worthwhile in the long term than futzing around with regexes. – Jeroen Mostert Jun 27 '19 at 10:39
  • 3
    I'm going to go ahead and ask folks to stop opining on what *human* language to program in; it isn't pertinent to the question, and the question isn't at all impacted by the choice. Going on and on about it walks a thin line of propriety. – Marc Gravell Jun 27 '19 at 10:41

1 Answers1

4

Returns true for me; probably you didn't use @"...", so the escape tokens (\b etc) aren't what you think they are:

Console.WriteLine(Regex.IsMatch(
    @"INNER JOIN t_hat_meisaimidasi AS MM (READCOMMITTED, NOLOCK) WHERE ( AND hat_kanri_no = ?",
    @"(?i)^(?!.*\bWITH\b).*\(\s*.*\s*\b(INDEX|FASTFIRSTROW|HOLDLOCK|SERIALIZABLE|REPEATABLEREAD|READCOMMITTED|READUNCOMMITTED|ROWLOCK|PAGLOCK|TABLOCK|TABLOCKX|NOLOCK|UPDLOCK|XLOCK|READPAST)\b\s*.*\s*\)"));

Note: "\b" is a string of length 1 that contains a backspace character; @"\b" is a string of length 2 that contains a slash and a b. When dealing with regex, you almost always want to use a verbatim string literal (@"...").

To make it even better: Visual Studio will use colorization to tell you when you're getting it right:

enter image description here

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900