Reputation: 161
I want to allow some special characters like (,),\,_,.,
etc
and emojis is denoted by [\u0000-\u007F]+
Valid names are
"🎊❤️\U0001f923\U0001f923😊😊🎉🎉😍😍"
"12333🎊❤️\U0001f923\U0001f923😊😊🎉🎉😍😍.txt"
"123()-213🎊❤️\U0001f923\U0001f923😊😊🎉🎉😍😍.txt"
Invalid specialcharacters should be replaced with ""
"123^&*()!@#$🎊❤️\U0001f923\U0001f923😊😊🎉🎉😍😍" should be
"123()🎊❤️\U0001f923\U0001f923😊😊🎉🎉😍😍"
This regex does it for some special characters
string filename = "12%&^%^&% \U0001f973\U0001f973.xlsx"
string output = Regex.Replace(filename, @"[^\w\s\.\\[\]()|_-]+", "");
prints "12 .xlsx"
For unicode characters (like emojis)
string output = Regex.Replace(filename, @"[\u0000-\u007F]+", "");
prints"\U0001f973\U0001f973"
While combining i want
"12 \U0001f973\U0001f973.xlsx"
I have tried Test 1
string output = Regex.Replace(filename, @"[^\w\s\.\\[\]()|_-]+|[^\u0000-\u007F]+", "");
"12 .xlsx" // but no luck
Test 2
string output = Regex.Replace(filename, @"[^-\w\s\.\\\[\]()|_\u0000-\u007F]+", "");
prints "";
Upvotes: 0
Views: 118
Reputation: 8462
You need the "opposite" of your unicode range in order to be able to add it to your negated character class. Try:
[^\u0080-\uFFFF\w\s\.\\[\]()|_-]+
Upvotes: 1