Monthly Archives: December 2014

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.