Regex in text expansion?

I use TextExpander (among other tools), but every once in a while I can’t figure out how to implement an expansion I’d like.

Example: any time I type 2-3 digits and then bpma, expand that to the digits followed by bpm average

With regex, I could match with something like: \d{2,3}bpma – but I don’t think TextExpander does regex

Is there some way to do this? If not with TextExpander, then KM, BTT (which I’m trialing … not much use yet tho) or one of the other expansion tools like Typinator or Expanso?

I’m pretty sure I could do it in Hammerspoon (which I use for some other automations), but of course it’s easier if someone has already done the work…

Why not just have bpma expand to bpm average since you are typing the digits anyway?

Alternatively, you could have bpma expand to a fill in where you type the digits into the pop-up as part of the expansion.


Text Expander supports JavaScript. Smile even have a guide…

1 Like

That’s what I’ve done for now. But I occasionally come up with other expansions where regex would help

Hm. That could be helpful.

I should have said … and doesn’t require me to use JavaScript! :joy: I don’t love JavaScript… but you’re right, that would let me do this and just about anything else I need…

If I understand you correctly, you’re looking to write a trigger that includes some regex (i.e. so that whether you type 123bpma or 456bpma both will trigger the same expansion which will use regex capture groups to expand to 123 bpm average and 456 bpm average respectively.

I don’t think that’s possible with any text expansion product I’ve seen. (My guess is that the potential for trigger conflicts expands exponentially if the triggers themselves have regex.) I’d use TextExpander fill-ins for this instead.

Alternatively, if the expansion doesn’t need to happen in real time, you could just write a script to process and expand a text file after the fact.

Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.



Twenty characters

1 Like

I might be misunderstanding what you’re trying to do. It sounds like you want to type something like 85bpma and have that expand to 85 bpm average. In which case, you shouldn’t need to do anything with regex or scripting. You basically just need to trigger on bpma and change the delimiters so that it’ll expand when not preceded by a space (and then if you want there to be a space between the number and bpm once expanded, just put a space at the start of the expansion).

See the instructions here.

Edit: although I’ve just realised you might be saying you explicitly don’t want it to expand if not preceded by the numbers. Are there situations where you want to type bpma and have it not expand? In which case it may need a bit more thinking about.


Here’s a more robust solution using Keyboard Maestro.

This uses a regex on the trigger, so it will only fire if the string starts with two or three digits, followed by bpma.

I’m capturing the number into a variable here, which then opens up possibilities beyond a simple expansion. You could, for example, do a calculation on the bpm value and output something into the expansion based on that:



Espanso supports regex triggers and ‘named groups’. Use it often. Huge fan!


Typinator also supports RegEx natively.

1 Like