0

I have this regular expression to check if a DLL have a prodcedure

var expReg = "(((ALTER|CREATE|OR|REPLACE| )*)+)?PROCEDURE.*";    
return (Regex.IsMatch(textProcedure, expReg, RegexOptions.IgnoreCase)

I use it with SQL Server or Oracle DLL.

Sometimes works, and sometime have a infinite loop.

What is the problem? Any help is greatly appreciated.

Victor Sanchez
  • 583
  • 9
  • 28
  • What do you want to match? Please, give some sample strings and expected result. – Toto Dec 12 '14 at 14:44
  • possible duplicate of [Why my Python regular expression pattern run so slowly?](http://stackoverflow.com/questions/27448200/why-my-python-regular-expression-pattern-run-so-slowly) – ivan_pozdeev Dec 12 '14 at 17:17

1 Answers1

2

A pattern of the form (X*)+ is a very bad idea in regex, because it can cause catastrophic backtracking. This kind of pattern forces the regex engine to try all the possible combinations before failing. This can take a long time.

In your case, I think the * is not required, simply use:

(?:ALTER|CREATE|OR|REPLACE|\s)+PROCEDURE

I've removed the ? too because otherwise the mere presence of the word PROCEDURE would be sufficient for the pattern to match.

This can be further simplified to:

(?:ALTER|CREATE|REPLACE)\s+PROCEDURE

Since it should be sufficient to match REPLACE PROCEDURE in the string CREATE OR REPLACE PROCEDURE.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
  • Thanks Lucas. You send a solution, and a cause of my poor regular expresion. I think that your solution can help some people. – Victor Sanchez Dec 15 '14 at 14:14