Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using RegEx to replace invalid characters

Tags:

c#

regex

I have a directory with lots of folders, sub-folder and all with files in them. The idea of my project is to recurse through the entire directory, gather up all the names of the files and replace invalid characters (invalid for a SharePoint migration).

However, I'm completely unfamiliar with Regular Expressions. The characters i need to get rid in filenames are: ~, #, %, &, *, { } , \, /, :, <>, ?, -, | and "" I want to replace these characters with a blank space. I was hoping to use a string.replace() method to look through all these file names and do the replacement.

So far, the only code I've gotten to is the recursion. I was thinking of the recursion scanning the drive, fetching the names of these files and putting them in a List<string>.

Can anybody help me with how to find/replace invalid chars with RegEx with those specific characters?

like image 647
yeahumok Avatar asked Jun 09 '10 19:06

yeahumok


People also ask

Can regex replace characters?

They use a regular expression pattern to define all or part of the text that is to replace matched text in the input string. The replacement pattern can consist of one or more substitutions along with literal characters. Replacement patterns are provided to overloads of the Regex.

Which regexp pattern and function can be used to replace special characters and numbers in a string?

The Oracle REGEXP_REPLACE() function replaces a sequence of characters that matches a regular expression pattern with another string. The REGEXP_REPLACE() function is an advanced version of the REPLACE() function.


1 Answers

string pattern = "[\\~#%&*{}/:<>?|\"-]"; string replacement = " ";  Regex regEx = new Regex(pattern); string sanitized = Regex.Replace(regEx.Replace(input, replacement), @"\s+", " "); 

This will replace runs of whitespace with a single space as well.

like image 117
Vivin Paliath Avatar answered Oct 05 '22 17:10

Vivin Paliath