Skip to main content

Command Palette

Search for a command to run...

TIL capitalising letters with regex

Fixing a code analysis auto-fix which made acronyms less clear

Updated
2 min read
TIL capitalising letters with regex

Background

My regex skills have improved since I wrote Just enough regex to get going, though I still think it's a good starting point for a developer who is new to regex.

In that article I wrote this:

start simple so that you can get value from using regex in simple use cases. Learn more when you need it.

I stand by that advice, and I am still learning extra regex tips & tricks as I need them.

Problem

I recently came across code where a code analysis auto-fix had messed up some capitalisation. There were comments which had begun with a capitalised acronym, but the auto-fix converted the first letter to lower-case.

// before:
/// <summary>
/// ABCD identifier.
/// </summary>
public int AbcdId { get; set; }

// after:
/// <summary>
/// Gets or sets aBCD identifier.
/// </summary>
public int AbcdId { get; set; }

In the context of this codebase, ABCD is a known acronym with a specific meaning. This change begs the question whether an aBCD is somehow different from an ABCD. In my opinion the change makes the comment less useful, not more useful.

Solution

This felt like something which could be fixed easily with a regex find & replace, but I didn't know if regex could capitalise a lowercase letter. Happily, it can!

The find pattern is the string "Gets or sets ", then a single lowercase letter, then a single uppercase letter.

Gets or sets ([a-z])([A-Z])

The replace pattern uses \u to uppercase the following character, which is the captured lowercase letter in this case.

Gets or sets \u\(1\)2

As always there's probably a caveat that not all regex engines support the same features and syntax, so I can't promise this works globally, but it worked for me.