Regular expression in Infinite Loop

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

The below regular express can sometimes cause an infinite loop. Anyone know
what could cause this?
string RegexSig = @"To:*\s*(?<address>\S+@\S+)*(\s|\S)*did not reach the
following recipient";

Also shouldn't the framework have some type of loop protection? This will
run forever.

Bob
 
Bob said:
The below regular express can sometimes cause an infinite loop. Anyone know
what could cause this?
string RegexSig = @"To:*\s*(?<address>\S+@\S+)*(\s|\S)*did not reach the
following recipient";

Could you supply a string that causes the regex to go into an infinite
loop? Your regex certainly does match itself (and some trivial
variants) in a finite amount of time.

I don't see anything that looks obviously infinite, but @"(\s|\S)*" is
basically just an expensive way to match @".*" and might take a very
long time in a very long string. (Matching @".*foo" can be optimized -
jump to the end and scan backward for "foo" - in a way that
@"(\s|\S)*foo" almost certainly isn't.)

Beyond that, I suspect you want @"To:?", not @"To:*", and you probably
want to handle addresses like "To: Fred Opper <[email protected]>".

--

..NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>

Delphi skills make .NET easy to learn
Being printed - in stores by June
 
Back
Top