title: Regex: convert camel case to all caps with underscores
body: <p>What regular expression can be used to make the following conversions?</p>
  
  <pre><code>City -&gt; CITY
  FirstName -&gt; FIRST_NAME
  DOB -&gt; DOB
  PATId -&gt; PAT_ID
  RoomNO -&gt; ROOM_NO
  </code></pre>
  
  <p>The following almost works - it just adds an extra underscore to the beginning of the word:</p>
  
  <pre><code>var rgx = @"(?x)( [A-Z][a-z,0-9]+ | [A-Z]+(?![a-z]) )";
  
  var tests = new string[] { "City",
                             "FirstName",
                             "DOB",
                             "PATId",
                             "RoomNO"};
  
  foreach (var test in tests)
      Console.WriteLine("{0} -&gt; {1}", test, 
                         Regex.Replace(test, rgx, "_$0").ToUpper());
  
  
  // output:
  // City -&gt; _CITY
  // FirstName -&gt; _FIRST_NAME
  // DOB -&gt; _DOB
  // PATId -&gt; _PAT_ID
  // RoomNO -&gt; _ROOM_NO
  </code></pre>