Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression Crashes .NET

Status
Not open for further replies.

JCruz063

Programmer
Feb 21, 2003
716
US
Hi All,
I'm using Visual Studio 2002 with .NET 1.0. Users have the ability to upload files to my ASP.NET application, and for that, I use the HtmlInputFile control. I have the following code below:

Code:
string fileName = Regex.Match(inputFileUpload.PostedFile.FileName.Trim(), @"((?: )|(?:\w+))+\.xls$").Value;

This code has always worked. Now, however, I have a file named "xyz12-8-05.xls", and my application freezes when it reaches the line above with this file. Anybody knows what's going on?

Thanks.

JC

_________________________________
I think, therefore I am. [Rene Descartes]
 
can you post your error message?

For debugging, I would break that line into several lines, ie, assign the postedfile.filename to a string, and test it (messagebox or write to screen, etc) to make sure its getting what you expect, before trying the regex.match

 
Thanks Neil...
But you see, that's the thing: there is no error message.

I was able to fix the problem, though, by changing the regular expression pattern. The new pattern is:

^[^\s|\.][\\\/\:\*\?\"\<\>\|]+[^\s]\.xls

This is what I should have had there to begin with. To be honest, I obtained the other pattern from the internet when I was when I was first learning regular expressions, and I wasn't able to see the problem. The above pattern fixes the trick, although I still don't know why the other pattern would freeze the machine.

Thanks again.

JC

_________________________________
I think, therefore I am. [Rene Descartes]
 
ya thats interesting, perhaps theres a bug with throwing exceptions in the regex class.

Can you force error messages with other parameters? I'd be worried if a class was prone to freezing up without throwing an exception.
 
What do you mean by "freezing"? Have you looked at the task manager while it is frozen for 100% CPU? There may just be a subtle interaction between that particular pattern, that file name and the .NET 1.0 regex engine that's throwing the program into an endless loop. You wouldn't see an exception from something like that.

If you could, it would be interesting to recompile your code under .NET 1.1 to see if it behaves the same way.

Jeff
[purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day

"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me
 
Thanks Jeff...

You're right. It didn't quite crash, but it took a long time to execute.

What I ended up doing was I changed the file name to see how the regex would react. I have a little program that allows me to test regular expressions (I took it from Francesco Balena's Programming Visual Basic's .NET book), and I plugged the pattern into it, and tried out different file names. I noticed that the longer the name of the file, the longer it took the program to show me the matches. Also, when I used different characters to name the file, it would take even longer. I'm not exactly sure why the pattern would behave in such a way. The new one doesn't suffer from that, though.

Thanks again.

JC



_________________________________
I think, therefore I am. [Rene Descartes]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top