Find all string literals in C# code files, but not the ones in comments

When trying to find string literals to translate in ourĀ rather largeĀ 59 project solution I found out it is not that easy to search for string literals in your code when you use the default code comments format.

You see, there is a default regular expression in Visual Studio which lets you search for string literals (eg. search for all strings that are between quotes). This regular expression is:

((".+?")|('.+?'))

This regular expression however will also find entries in code comments like this:

/// <summary>
/// Begins to invoke the action in the current controller context.
/// </summary>
/// <param name="callback">The callback.</param>
/// <param name="state">The state.</param>
/// <returns>
/// Returns an IAsyncController instance.
/// </returns>

It took me and a co-worker some time to come up with a regular expression to filter these out. Our result was:

(?=(^((?!///).)*$)).*((".+?")|('.+?')).*

I’m not entirely sure myself how it works. But it is based on the fact that we only want to know the entire line which contains the string literal (and not specifically only the string literal). So we match all lines that don’t start with whitespace followed by ///, then in those lines match the original regex.

2 thoughts on “Find all string literals in C# code files, but not the ones in comments

  1. Jose

    Hi,

    How could I filter those strings by size? I mean, only showing strings with more than 1000 characters (multiline strings)

    Reply
    1. Administrator Post author

      I’m afraid my regex knowledge only goes as far as this post did. If you want to do a character count you should do something like ((".{1000,}?")|('.{1000,}?')) for the default regex and (?=(^((?!///).)*$)).*((".{1000,}?")|('.{1000,}?')).* for the one that doesn’t return instances within documentation blocks. I have no idea how to do the newline check though.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.