Replace / Remove Regular Expression Characters (.NET)

I have a regex to test a string. But now I want to remove all characters that do not match my regular expression.

eg.

regExpression = @"^([\w\'\-\+])" text = "This is a sample text with some invalid characters -+%&()=?"; //Remove characters that do not match regExp. result = "This is a sample text with some invalid characters -+"; 

Any ideas on how I can use RegExpression to determine valid characters and remove all the rest.

Many thanks

+7
source share
3 answers

I believe that you can do this (whitelist characters and replace everything else) in one line:

 var result = Regex.Replace(text, @"[^\w\s\-\+]", ""); 

Technically, this will produce this: "This is an example of text with some invalid characters - +" which is slightly different from your example (extra space between - and +).

+11
source

Simple:

 var match = Regex.Match(text, regExpression); string result = ""; if(match.Success) result = match.Value; 

Deleting inconsistent characters is the same as storing matching characters. This is what we are doing here.

If it is possible that the expression matches several times in the text, you can use this:

 var result = Regex.Matches(text, regExpression).Cast<Match>() .Aggregate("", (s, e) => s + e.Value, s => s); 
+9
source

Thanks Replace characters if answer does not match I created a helper method for strips of unaccepted characters .

The permitted pattern must be in Regex format, expecting to be enclosed in square brackets. The function will insert the tilde after opening the scraper bracket. I expect that it may not work for all RegEx describing valid character sets, but it does work for the relatively simple sets that we use.

  /// <summary> /// Replaces not expected characters. /// </summary> /// <param name="text"> The text.</param> /// <param name="allowedPattern"> The allowed pattern in Regex format, expect them wrapped in brackets</param> /// <param name="replacement"> The replacement.</param> /// <returns></returns> /// // https://stackoverflow.com/questions/4460290/replace-chars-if-not-match. //https://stackoverflow.com/questions/6154426/replace-remove-characters-that-do-not-match-the-regular-expression-net //[^ ] at the start of a character class negates it - it matches characters not in the class. //Replace/Remove characters that do not match the Regular Expression static public string ReplaceNotExpectedCharacters( this string text, string allowedPattern,string replacement ) { allowedPattern = allowedPattern.StripBrackets( "[", "]" ); //[^ ] at the start of a character class negates it - it matches characters not in the class. var result = Regex .Replace(text, @"[^" + allowedPattern + "]", replacement); return result; } static public string RemoveNonAlphanumericCharacters( this string text) { var result = text.ReplaceNotExpectedCharacters(NonAlphaNumericCharacters, "" ); return result; } public const string NonAlphaNumericCharacters = "[a-zA-Z0-9]"; 
+1
source

All Articles