Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If text contains characters other than

Tags:

string

c#

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.

like image 647
Matthijs Avatar asked Dec 20 '25 23:12

Matthijs


2 Answers

You can make the following:

if (!Regex.IsMatch(token.Text, @"^[/{}\s]*$"))
{
  // your code
}

Alternative:

if (Regex.IsMatch(token.Text, @"[^/{}\s]"))
{
  // your code
}
like image 123
Ulugbek Umirov Avatar answered Dec 23 '25 13:12

Ulugbek Umirov


If you just want to check if there are other characters than those three you can use the efficient Enumerable.Except + Enumerable.Any:

static char[] comments = { '/', '{', '}', ' ', '\t' };

public static void IsCommentDisabledCodeComment(Class classItem, IfSQRules context)
{
    // ...
        if (token.Text.Except(comments).Any())
        {
            // something other 
        }
    // ...
}

However, that's a very naive approach which just answers your initial question. It does not take care about the order of the characters. It does also not treat tab- or new-line characters as white-space (as Char.IsWhiteSpace). If that's not sufficient you need a regex or loop.

Edit: Instead of LINQ you can also use the efficient String.IndexOfAny-method:

if (token.Text.IndexOfAny(comments) >= 0)
{
    // something other 
}
like image 44
Tim Schmelter Avatar answered Dec 23 '25 13:12

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!