c# - Regex to replace invalid characters -


i don't have experience regex using many chained string.replace() calls remove unwanted characters -- there regex can write streamline this?

string messytext = gettext(); string cleantext = messytext.trim()          .toupper()          .replace(",", "")          .replace(":", "")          .replace(".", "")          .replace(";", "")          .replace("/", "")          .replace("\\", "")          .replace("\n", "")          .replace("\t", "")          .replace("\r", "")          .replace(environment.newline, "")          .replace(" ", ""); 

thanks

try regex:

regex regex = new regex(@"[\s,:.;/\\]+"); string cleantext = regex.replace(messytext, "").toupper(); 

\s character class equivalent [ \t\r\n].


if want preserve alphanumeric characters, instead of adding every non-alphanumeric character in existence character class, this:

regex regex = new regex(@"[\w_]+"); string cleantext = regex.replace(messytext, "").toupper(); 

where \w non-word character (not [^a-za-z0-9_]).


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -