If text contains characters other than
c#, string
Solution
You can make the following:
if (!Regex.IsMatch(token.Text, @"^[/{}\s]*$"))
{
// your code
}
Alternative:
if (Regex.IsMatch(token.Text, @"[^/{}\s]"))
{
// your code
}
Problem
I am in the process of writing a StyleCop rule. As a part of this I am searching for strings. If they contain any text other than `'/', '{', '}'` and `whitespace` characters, I want to do something with them. How can I target only the strings that contain anything other than these characters? Mind you: they can also contain the above said characters; but if any other than these gets discovered I want them to be flagged. Edit: As requested, my progess on the rule so far. I am checking comments to see if they contain disabled code. Because this flags many lines of code with simply: `// {` (and others); I want such lines to be excluded. ``` public static void IsCommentDisabledCodeComment(Class classItem, IfSQRules context) { foreach (CsToken token in classItem.Tokens) { if (token.CsTokenType == CsTokenType.MultiLineComment || token.CsTokenType == CsTokenType.SingleLineComment) { if (token.Text != "// }" && token.Text != "// }" && token.Text != "// }" && token.Text != "//}" && token.Text != "// }" && token.Text != "//// }" && token.Text != "// }" && token.Text != "// }" && token.Text != "//// {" && token.Text != "// {" && token.Text != "// {" && token.Text != "// {" && token.Text != "// {" && token.Text != "// {" && token.Text != "// {" && token.Text != "//{") { if (token.Text.Contains("()") || token.Text.Contains("[]") || token.Text.Contains("{") || token.Text.Contains("}")) context.AddViolation(classItem, token.LineNumber, "ThereShouldNotBeAnyDisabledCode", token.Text); } } } } ``` What you see here is a really, really bad approach at achieving this, but this is obviously not something I want to use.