Rule #1 of Regex debugging

Rule #1 of Regex debugging

The regex engine isn’t hung. It will finish eventually.

However, “eventually” may not until after the heat death of the universe.

You either didn’t anchor your string, or you didn’t think clearly about how the quantifiers “*” or “+” work. Or both…

Comments (3)

  1. Maurits says:

    Does "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" match "a*b"?

  2. EricLippert says:

    Well, actually, we _did_ accidentally and not on purpose have some hanging bugs in the JScript/VBScript regex engine.  I fixed the last known one about six years ago, but that doesn’t mean that there aren’t more that we haven’t discovered yet.

    And of course, as you imply, the fix was to turn an actual infinite loop into a loop which is guaranteed to terminate in theory, but in fact could take arbitrarily long.

    The existence of such ill-behaved regular expression matches is very unfortunate for scripting denial-of-service scenarios. All of the "abort this script, it’s taking too long" mechanisms assume that it’s OK to stop running the script when it reaches the beginning of the next _statement_, not the next _script instruction_.