开发者

regex - Replace all dots,special characters except for the file extension

I want a regex in such a way that to replace the filename which contains special character开发者_开发问答s and dots(.) etc. with underscore(_) except the extension of the filename.

Help me with an regex


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, "_");


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.: \.[^.]*$


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);
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜