tags:

views:

436

answers:

4

I want a regex in such a way that to replace the filename which contains special characters and dots(.) etc. with underscore(_) except the extension of the filename.

Help me with an regex

+1  A: 

try this:

([!@#$%^&*()]|(?:[.](?![a-z0-9]+$)))

with the insensitive flag "i". Replace with '_'

The first lot of characters can be customised, or maybe use \W (any non-word)

so this reads as:

replace with '_' where I match and of this set, or a period that is not followed by some characters or numbers and the end of line

Sample c# code:

var newstr = new Regex("([!@#$%^&*()]|(?:[.](?![a-z0-9]+$)))", RegexOptions.IgnoreCase)
    .Replace(myPath, "_");
Luke Schafer
added c# code - originally didn't realise it was a c# question
Luke Schafer
sukumar
uh... yes it is. I mention twice, once on the third line, and once in the example code, to make it case insensitive
Luke Schafer
+2  A: 

Since you only care about the extension, forget about the rest of the filename. Write a regex to scrape off the extension, discarding the original filename, and then glue that extension onto the new filename.

This regular expression will match the extension, including the dot.: \.[^.]*$

Wayne Conrad
+1  A: 

Perhaps just take off the extension first and put it back on after? Something like (but add your own list of special characters):

static readonly Regex removeChars = new Regex("[-. ]", RegexOptions.Compiled);
static void Main() {
    string path = "ab c.-def.ghi";
    string ext = Path.GetExtension(path);
    path = Path.ChangeExtension(
        removeChars.Replace(Path.ChangeExtension(path, null), "_"), ext);
}
Marc Gravell
+1  A: 

Once you separate the file extension out from your string would this then get you the rest of the way?

keith