How To Use Text Case Conversion Meta characters

Keyboard Maestro 9 adds new text case conversion characters. These are typically seen in other languages in the replacement section of a regular expression search & replace. And they do indeed work nicely in the Search and Replace action, but they also work in any other token Text Field.

The Available Case Conversion Meta Characters are:

  • \U converts everything up to the next \L or \E to uppercase.
  • \L converts everything up to the next \U or \E to lowercase.
  • \u converts the next character to uppercase.
  • \l converts the next character to lowercase.
  • \U\l lowercase first, then uppercase.
  • \L\u uppercase first, then lowercase.
  • \E stop changing case.

You should not use \u after \U or \l after \L unless you terminate the sequence with \E first.

So for example, if you want to properly case a two word name in a variable “Full Name”, you might use an action like:

image

Search for (\w+)\s+(\w+) replace with \L\u$1 \L\u$2.

That is, find a pair of word character sequences separated by space characters, and replace them with the words lowercased, except the first character uppercased.

Or if you want to have a variable “Important” interpolated in a message in uppercase, you might do something like:

image

This would include the text from variable “Important” in uppercase.

Keyboard Maestro 9 also supports named capture groups (in OS X 10.13+), for example, with the variable “Example” holding something like “The name is Fred.”, this:

image

Search for The name is (?<Name>\w+). and replace with ${Name} is the name, and the name is really ${Name}!

would result in “Fred is the name, and the name is really Fred!”

Usually this would be overkill, but for a complex regex, it could be handy and help document what the regex capture groups are.

3 Likes

There's one text field that they don't seem to work in.

image

In this example the intention was to replace all the uppercase words, only the uppercase ones, with "X". Yes there's another way to do that but the point is this feature doesn't work here even though it conceivably could.

It seems that to make case-specific searches work I have to toggle the value after the "for" keyword. But then that applies to the whole line, and you can't use \U and \E to work with only parts of the line, which in the past would have been helpful to me.

Now that you've got \U, \l and \E, isn't it hypothetically conceivable that you could replace the options of "Regular Expression (case sensitive)" and "Regular Expression (ignoring case)" with just "Regular Expression"? Ie, by default "Regular Expression" would be "ignoring case" unless you used \U or \l which would make the search "case sensitive". Of course you won't want to do this because it would break everyone's macros for only a slight amount of power gain and consistency. I'm not asking for this change, I'm only asking to see if I understand the issue.

Regular expression fields do not process Keyboard Maestro \ meta characters, they process regular expression \ characters.

Even if it did, then \U\w would produce \W which would not be what you want at all.

1 Like