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 27332
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |