Does anyone have a good regex expression to replace any invalid filename
characters in a string? Those characters are:
/, \, :, *, ?, ", <, >, |
I have it right now with string.replace for each individual character,
but would like to do it all in one step if possible. Here's what I have
now:
title = title.Replace("?", "");
title = title.Replace(":", "");
title = title.Replace("\\", "");
title = title.Replace("/", "");
title = title.Replace("*", "");
title = title.Replace("\"", "");
title = title.Replace("<", "");
title = title.Replace(">", "");
title = title.Replace("|", "");
newFileName += title + targetFile.Extension;
targetFile.MoveTo(newFileName);
Thanks!
Greg 4 27312
GregMa <gr****@spamhole.com> wrote in
news:MP************************@msnews.microsoft.c om: Does anyone have a good regex expression to replace any invalid filename characters in a string? Those characters are: /, \, :, *, ?, ", <, >, |
I have it right now with string.replace for each individual character, but would like to do it all in one step if possible. Here's what I have now:
title = title.Replace("?", ""); title = title.Replace(":", ""); title = title.Replace("\\", ""); title = title.Replace("/", ""); title = title.Replace("*", ""); title = title.Replace("\"", ""); title = title.Replace("<", ""); title = title.Replace(">", ""); title = title.Replace("|", ""); newFileName += title + targetFile.Extension; targetFile.MoveTo(newFileName);
Greg,
Something like this should work:
newFileName = Regex.Replace(title, @"[?:\/*""<>|]", "");
Hope this helps.
Chris.
-------------
C.R. Timmons Consulting, Inc. http://www.crtimmonsinc.com/
In article <Xn**********************************@207.46.248.1 6>, crtimmons@X_NOSPAM_Xcrtimmonsinc.com says... GregMa <gr****@spamhole.com> wrote in news:MP************************@msnews.microsoft.c om:
Does anyone have a good regex expression to replace any invalid filename characters in a string? Those characters are: /, \, :, *, ?, ", <, >, | Greg,
Something like this should work:
newFileName = Regex.Replace(title, @"[?:\/*""<>|]", "");
Hope this helps.
Worked like a charm, Thanks! I had an extra ^ at the beginning, that's
why my attempt didn't work. Many thanks!
Greg
Chris R. Timmons wrote: GregMa <gr****@spamhole.com> wrote in news:MP************************@msnews.microsoft.c om:
Does anyone have a good regex expression to replace any invalid filename characters in a string? Those characters are: /, \, :, *, ?, ", <, >, |
I have it right now with string.replace for each individual character, but would like to do it all in one step if possible. Here's what I have now:
title = title.Replace("?", "");
<snip> Something like this should work:
newFileName = Regex.Replace(title, @"[?:\/*""<>|]", "");
Or possibly use the String.Trim function instead which can trim off a
list of chars. ... Regex may be a bit of overkill in this instance.
I'd also make sure you don't miss anything by using the InvalidPathChars
property of
the Path class. Some strange characters can find their way into names like
backspaces,
nulls, etc...
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"@my.email> >" <I.hate,spam\"@my.email> wrote in message
news:c2**********@otis.netspace.net.au... Chris R. Timmons wrote: GregMa <gr****@spamhole.com> wrote in news:MP************************@msnews.microsoft.c om:
Does anyone have a good regex expression to replace any invalid filename characters in a string? Those characters are: /, \, :, *, ?, ", <, >, |
I have it right now with string.replace for each individual character, but would like to do it all in one step if possible. Here's what I have now:
title = title.Replace("?", ""); <snip> Something like this should work:
newFileName = Regex.Replace(title, @"[?:\/*""<>|]", "");
Or possibly use the String.Trim function instead which can trim off a list of chars. ... Regex may be a bit of overkill in this instance. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lawrence |
last post by:
When users enter urls or other long strings it can destroy the
formatting of a page. A long url, posted in a comment, can cause page
distortions that make the page unreadable, till the website...
|
by: alphatan |
last post by:
Is there relative source or document for this purpose?
I've searched the index of "Mastering Regular Expression", but cannot
get the useful information for C.
Thanks in advanced.
--
Learning...
|
by: Hardy Wang |
last post by:
Hi:
I have a XML like
<?xml version="1.0" ?>
<object>
<comments>www.site.com/page.aspx?param1=value1¶m2=value2</comments>
</object>
Since "&" is invalid in XML, I need to replace all "&"...
|
by: clintonG |
last post by:
I'm using an .aspx tool I found at but as nice as the interface is I
think I need to consider using others. Some can generate C# I understand.
Your preferences please...
<%= Clinton Gallagher
...
|
by: Chris Anderson |
last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text
For example, you'd think that thi...
|
by: Whitless |
last post by:
Okay I am ready to pull what little hair I have left out.
I pass the function below my String to search, my find string (a regular
expression) and my replace string (another regular expression)....
|
by: Bryan Young |
last post by:
I want to generate a usable filename (no path, drive letter, or extension)
from strings from various sources (dates, database fields, user entry, etc).
I'm trying to use the following line and...
|
by: Mick Walker |
last post by:
Hi All,
I would like to know how I can limit users to only registering usernames
which have alphanumberic characters and one underscore.
From what I understand is I can use a regex to do this....
|
by: Phil Barber |
last post by:
I am using Regex to validate a file name. I have everything I need except I
would like the dot(.) in the filename only to appear once. My question is it
possible to allow one instance of character...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |