views:

126

answers:

1

I have the following regular expression for validating a file name:

^(([a-zA-Z]:|\)\)?(((.)|(..)|(. %5D">^\/:*\?"\|<>. |([^\/:*\?"\|<>][^\/:*\?"\|<>. ]))?))\). %5D">^\/:*\?"\|<>. |([^\/:*\?"\|<>]*[^\/:*\?"\|<>. ]))?$

I can get it to work in VB.NET but not C#. I can't figure out why it works in one but not the other.

VB code:

Regex.Matches("c:\temp\abc.exe", "^(([a-zA-Z]:|\\)\\)?(((\.)|(\.\.)|([^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?))\\)*[^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?$")

C# code:

Regex.Matches("c:\temp\abc.exe", @"^(([a-zA-Z]:|\\)\\)?(((\.)|(\.\.)|([^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?))\\)*[^\\/:\*\?""\|<>\. ](([^\\/:\*\?""\|<>\. ])|([^\\/:\*\?""\|<>]*[^\\/:\*\?""\|<>\. ]))?$");

As far as I can tell the patterns are identical in both languages with escaping. When I run the VB code I get a match. When I run the C# code I get nothing.

Can anyone see what I'm missing?

+16  A: 

Don't you need to also escape the filename in C#? E.g:

@"c:\temp\abc.exe"
Matthew Flaschen
Just tested it and it's correct.
Vinko Vrsalovic
Thanks! This was driving me nuts and it was the simplest thing hahaha. Feel stupid now :)
GiddyUpHorsey