Created
April 19, 2018 14:42
-
-
Save lsancho/462d3f338d5037b6cbbf3adc78584bb2 to your computer and use it in GitHub Desktop.
used to remove characters not allowed in product names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text.RegularExpressions; | |
namespace Utils.Text | |
{ | |
public static class NotAllowedCharacters | |
{ | |
private const string AccentsCharactersAllowed = "áéíóúâêîôûãõàèìòùäëïöüçñÁÉÍÓÚÂÊÎÔÛÃÕÀÈÌÒÙÄËÏÖÜÇÑ`´"; | |
private const string SpecialCharactersAllowed = "-+÷\\/><=≠.,:;\'\"!¡?¿{}[]()@#*&€$%*°ºª²³⅓⅔⅛⅜⅝⅞_"; | |
public static bool Contains(string text) | |
{ | |
return !string.IsNullOrWhiteSpace(text) && (!Regex.IsMatch(text, PatternAllowedCharacters) || Regex.IsMatch(text, PatternDoubleSpaces)); | |
} | |
public static string Replace(string text, char? replacement = null) | |
{ | |
return string.IsNullOrWhiteSpace(text) ? text : Regex.Replace(Regex.Replace(text, PatternNotAllowedCharacters, ""), PatternDoubleSpaces, " "); | |
} | |
private static string PatternAllowedCharacters => $"^[{ToRegexString(SpecialCharactersAllowed)}{ToRegexString(AccentsCharactersAllowed)} a-zA-Z0-9]+$"; | |
private static string PatternNotAllowedCharacters => $"[^{ToRegexString(SpecialCharactersAllowed)}{ToRegexString(AccentsCharactersAllowed)} a-zA-Z0-9]"; | |
private static string PatternDoubleSpaces => $"\\s\\s+"; | |
private static string ToRegexString(string text) | |
{ | |
return text | |
.Replace("(", "\\(") | |
.Replace(")", "\\)") | |
.Replace("[", "\\[") | |
.Replace("]", "\\]"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment