locked
VS Replace all uppercase to all lowercase

    Question

  • I want to use Find and Replace in Visual Studio to find all "identifiers" (begins with letter, contains letter/number) that are entirely uppercase (all letters in the identifier are uppercase). And replace it with the same identifier, all lowercase.

    i.e. ONE -> one, TWO2 -> two2, but Twelve is left alone, as is TwentyOne.

    Possible?
    Tuesday, August 18, 2009 7:07 PM

All replies

  • Yes.

                string pattern = @"\b[\w-[\p{Ll}]]+\b";
                string test = "one One ONE TWO2 Three fOur F5IVE";
                Console.WriteLine("{0}", Regex.Replace(test, pattern, (m) => m.Value.ToLower(), RegexOptions.ECMAScript));
    

    Some explanation.  The pattern I'm using relies upon the RegexOption.ECMAScript with makes \w match only "identifier" characters or [0-9a-zA-Z].  Otherwise, \w match several other characters.  Using the -[\p{Ll}] is just a fancy way of saying -[a-z] meaning remove lower case from the matchable characters.  So the pattern finds only symbols with uppercase or digit (I could have said \b[A-Z0-9]+\b instead, but I chose a more educational form).  The Replace method uses a simple delegate (a Lambda expression in this case) to replace the match with the lower case version of the match.

    Caveat.  This pattern picks up "symbols", not just "identifiers", so words like IF THEN ELSE will get converted to if then else.  Your list of reserved words can be added to the pattern if needed.

    If you are planning on searching source code, you will need to add the RegexOptions.Multiline option to span multiple lines, otherwise you will have to apply the Replace() one line at a time.
    Les Potter, Xalnix Corporation, Yet Another C# Blog
    Tuesday, August 18, 2009 7:30 PM
  • Les,

    I think he was wanting to do this in Visual Studio using the Find and Replace in Visual Studio, not in C# code. 
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiTwitterLinkedInForumsBrowser
    Tuesday, August 18, 2009 7:31 PM
  • It's true, I was looking for strings to plug into VS's Find and Replace dialog.

    I suppose I can write a program to do it..

    Thank you.
    Tuesday, August 18, 2009 7:37 PM
  • The VS Regex is pretty limited, Here is the documentation


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Tuesday, August 18, 2009 7:43 PM
  • I should have read more carefully.  The VS version of Find/Replace using Regex is not as powerful as the Framework's.  Using VS Regex, you can certainly find the symbols and identifiers, but differentiating them as all uppercase or not, and replacing matches with lowercase, is not possible.

    (ps.  My proposed solution breaks on strings like 5FIVE, oops!)
    Les Potter, Xalnix Corporation, Yet Another C# Blog
    Tuesday, August 18, 2009 7:46 PM
  • You can do Ctrl + F, then use :Al*:Nu to find them all, you can replace each one sequentially

    Ensure Regular Expressions is chosen.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Edited by JohnGrove Tuesday, August 18, 2009 7:53 PM
    Tuesday, August 18, 2009 7:51 PM
  • I'll accept "not possible" as an answer as I have no interest in spending my time reading the manual (I could write a program to do it in less time than it would take to read the first paragraph).

    5FIVE is not a problem as that's not a valid identifier and would not appear in the files I want to make less ugly (it's actually a bunch of very large T-SQL files).
    Tuesday, August 18, 2009 7:52 PM
  • You can do Ctrl + F, then use :Al+:Nu to find them all, you can replace each one sequentially

    Ensure Regular Expressions is chosen.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com

    That's not bad if they are short files. I have "to lower" bound to Ctrl+L, so it would be a matter of repeately pressing F3, Ctrl+L. But in this case, there's way too much code.
    Tuesday, August 18, 2009 7:53 PM
  • I would write a small utility to do that like Les demonstrated
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Tuesday, August 18, 2009 7:55 PM
  • You might be able to achieve this with a VS Macro.  I hadn't thought of Macros until you mentioned you have Ctrl+L bound to ToLower.  I would guess tha the Find/Ctrl+L/Find Next can be Macro'ed.  But, I've never written macros in VS, so I you're on your own (but share your story with us if you get it worked out).


    Les Potter, Xalnix Corporation, Yet Another C# Blog
    Tuesday, August 18, 2009 8:04 PM
  • :Al+:Nu

    Doesn't find anything. I'm certain I checked Use: Regular Expressions. I toggled "match case" and "match whole word" off/on, but nothing.
    Tuesday, August 18, 2009 8:11 PM
  • <[A-Z][A-Z0-9]+>

    Will find the identifiers you want,  but it will also find them inside of comments and strings.
    Les Potter, Xalnix Corporation, Yet Another C# Blog
    Tuesday, August 18, 2009 8:27 PM
  • :Al+:Nu

    Doesn't find anything. I'm certain I checked Use: Regular Expressions. I toggled "match case" and "match whole word" off/on, but nothing.

    What I meant to say was:

    <{:Al+:Nu}>



    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Tuesday, August 18, 2009 11:21 PM