-1

I have a regular expression i.e.

[A-Z]\w*(\s+([A-Z0-9]\w*|-[a-z]?|and|or|of|of the))*(?=\s*,?\s*\(?\s*as defined below\s*\)?)

and it is working fine for me for running it on a word document. But changing it to

[A-Z]\w*(\s*([A-Z0-9]\w*|-[a-z]?|and|or|of|of the))*(?=\s*,?\s*\(?\s*as defined below\s*\)?)

with just changing \s+ to \s* takes too much time. and does not return the result even in 10 minutes(waiting more than that is quite vain). even

[A-Z]\w*(\s([A-Z0-9]\w*|-[a-z]?|and|or|of|of the))*(?=\s*,?\s*\(?\s*as defined below\s*\)?)

also not giving result in less than 10 minutes.

Moreover i am testing it for

Registration Rights has agreed not to
 exercise such rights until after expiration of the Lock-Up Period (as defined below)

and expecting Lock-Up Period as result

  • 1
    Check this qn http://stackoverflow.com/questions/12499764/regex-taking-surprisingly-long-time – Srikanth May 13 '17 at 13:41
  • 2
    Let's first clarify if this is a C# related problem or catastrophic backtracking. Test on an external system for example http://regexr.com – Cee McSharpface May 13 '17 at 13:42
  • 1
    You should not make that whitespace pattern optional. It will lead to catastrophic backtracking. – Wiktor Stribiżew May 13 '17 at 13:45
  • The regular expression you shared is invalid. Please share your *real* regex. – Wiktor Stribiżew May 13 '17 at 14:23
  • its updated now. yupe, there were some typo mistakes. – Muhammad Aqeel May 13 '17 at 14:52
  • About your first pattern, you say that "it is working fine for me", but then you state that you expect a match of `Lock-Up Period`, but the first pattern matches only `Up Period`. You can't get good help if you aren't careful about the details you post. – C Perkins May 13 '17 at 15:58
  • @CPerkins yupe i think i didint communicate well. Actually problem is first regex is not capturing `Lock-Up Period` because of one space limitation. I want to make whitespace optional. The whole problem is just with whitespace. – Muhammad Aqeel May 16 '17 at 05:27
  • Okay, so you selected the Parallel programming answer already. Did that really fix the problem? – C Perkins May 16 '17 at 05:41
  • actually that helped a bit. i just wait for that thread to complete its working. but i think performance can be an issue in future. still looking for some solid answer. Moreover, i am putting my regex in a template and then matching from that template. @CPerkins do you have any answer? – Muhammad Aqeel May 16 '17 at 05:52
  • `private static string asDefinedBelow = @"my regex here";` and `internal static SearchTemplate TermUseAsDefinedBelow = new SearchTemplate(asDefinedBelow, ArtifactType.TERMUSEASDEFBELOW, false);` are written in Patterns.cs and using it as `artifacts = Search.SimpleSearch(_textUnits, Patterns.TermUseAsDefinedBelow, reporter.Children[1]);` in another class. – Muhammad Aqeel May 16 '17 at 05:56
  • So do you have access to the actual code which creates and calls the `Regex` object, perhaps inside call to `SimpleSearch()`? Also, do you know if `SearchTemplate()` changes or adds to the original pattern you specify in `asDefinedBelow`? This is important, because in my initial testing of your pattern, the time and success of the pattern depended upon the tool I used to run it and what options I selected, like "Ignore Pattern Whitespace" or "Compiled", etc. If I run a simple test in c#, calling the Regex.Test or Regex.Match directly, it is successful and returns in milliseconds. – C Perkins May 16 '17 at 06:31
  • i just changed it to below regex and its working fine. Thanks for your cooperation. `[A-Z][\w-]*(\s+((of the|of|and|or)\s+)?[A-Z0-9][\w-]*)*(?=\s*,?\s*\(\s*as\s+defined\s+below\s*\))` – Muhammad Aqeel May 17 '17 at 06:33
  • @CPerkins please add this answer below and i will mark it. i cant add any answer for the time due to other issue. – Muhammad Aqeel May 17 '17 at 06:36

1 Answers1

0

Please try use Parallel programming it will shift working load on all available cores. Put your code in a method and call that method from parallel like``

Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork());

See
Parallel Programming

  • 2
    Why is this an acceptable answer for debugging a regular expression? Simply placing a faulty regex in another thread doesn't solve the problem. – C Perkins May 13 '17 at 15:50