Re: invalid file name chars (RegEx)

"Efran Cobisi, cobisi.com" <[email protected]> Mon, 25 Jun 2007 22:02:26 +0200
Newsgroups gmane.comp.windows.devel.dotnet.cx
Organization cobisi.com
Message-ID <[email protected]>
Hi Peter,

In your expression, the double backslash actually will be resolved as a
single backslash by the C# compiler and would act as an escape character
for the next one, the forward slash. To overcome this annoiance, you
should either prefix the expression with the @ character or repeat the
double backslash sequence one more time. For the same reason, the input
expression contains a backspace escape sequence (\b) which isn't
contained in your expression: solving this one is pretty easy, just add
the same sequence to the regex expression.

Here's a possible solution to the problem:

Regex regex = new Regex (@"[\\/:*?<>|\b]" );

Hope this helps.

--
Efran Cobisi
http://www.cobisi.com

Peter Osucha wrote:
> I'm still playing with regular expressions - though I don't get enough
> time to devote to the subject.
>
>
>
> Anyway, I have a string of characters and all I'm trying to do is make
> replace all the invalid file name chars with "" to I can use the
> replaced name as a filename.  I am trying to do this with a simple
> regular expression statement but this doesn't seem to be working.
>
>
>
>             Regex regex = new Regex ("[\\/:*?<>|]" );
>
>             string input = "Test:*\\is/?<a\bad?.name";
>
>             Console.WriteLine(regex.Replace(input,""));
>
>
>
> Can someone please tell me what I'm doing wrong here?
>
>
>
> Thanks,
>
> peter
>
>
>
>
>
>
> ===================================
> This list is hosted by DevelopMentorĀ®  http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>
>
>

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com