Re: invalid file name chars (RegEx)
Peter Osucha <[email protected]> Tue, 26 Jun 2007 20:48:50 -0400
| Newsgroups | gmane.comp.windows.devel.dotnet.cx |
|---|---|
| Message-ID | <[email protected]> |
Efran, Thanks - that was the solution I was looking for. I often forget about the '@' character - still holding over from my VB6 days. Steve and Alan, it is both a learning exercise and a need. :-) I played with some simpler regexes to get to the point I was at - those escape chars always seem to get me. Thank you both for the comments as well. Peter -----Original Message----- From: Discussion relating to the specifics of the C# and Managed C++ languages [mailto:[email protected]] On Behalf Of Efran Cobisi, cobisi.com Sent: Monday, June 25, 2007 4:02 PM To: [email protected] Subject: Re: [DOTNET-CX] invalid file name chars (RegEx) 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 =3D 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 =3D new Regex ("[\\/:*?<>|]" ); > > string input =3D "Test:*\\is/?<a\bad?.name"; > > Console.WriteLine(regex.Replace(input,"")); > > > > Can someone please tell me what I'm doing wrong here? > > > > Thanks, > > peter =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D This list is hosted by DevelopMentor=AE http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com