Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要清理文件名。所以我有这个代码:
//\W_ is any non-word character (not [^a-zA-Z0-9_]). Regex regex = new Regex(@"[\W_]+"); return regex.Replace(source, replacement);
这很好用,但现在我不想删除减号 (-),所以我将正则表达式更改为:
[\W_^-]+
但这不起作用。我错过了什么?
尝试使用这个正则表达式:
[^\w-]+
编辑 :
似乎正确的正则表达式是:
[^a-zA-Z0-9-]+
只是颠倒你想要的和你不想要的:
用于此的 RegexPal 链接。