Remove Invalid Characters With RegEx – C#

We integrated CRM with SharePoint and decided to create a document set for every entity in CRM. I wrote an application to automate the process using C# and ran into an issue with entity names ending with periods (e.g. Company Name, Inc.).

During the create action I replace invalid characters using this RegEx:

@"[\*\?\|\\\t/:""'<>#{}%~&]|[\. ]$"

It looks like this:


DocumentSet docSet = DocumentSet.Create(parentFolder, Regex.Replace(DocumentSetName.Trim(), @"[\*\?\|\\\t/:""'<>#{}%~&]|[\. ]$", ""), docsetCT.Id, properties, true);