Search code examples
regexkeyboard-maestro

Regex \b followed by symbol in Keyboard Maestro


I'm sure there's a really simple answer to this, but I can't find it!

In Keyboard Maestro, I'm trying to set the trigger as a regular expression of semicolon followed by one of a few characters, like this:

;[.,\s]

When I put it like that, it works, but I only want the trigger to fire when the semicolon is on its own (at the beginning of a sentence, or after a space). I would think this would do the trick:

\b;[,.\s]

...but when I put the boundary character in, it doesn't work. What am I doing wrong? Thanks!

(I should add that the boundary character works fine when followed by an alphanumeric character, so it seems to just be an issue with symbols)


Solution

  • You should use the opposite construct as there is no word boundary between a space or start of string and a semi-colon:

    \B;[,.\s]
    ^^
    

    Here, \B is a non-word boundary that matches at all the locations where a word boundary does not match. In this specific case, ; will be matches only at the start of the string or if preceded with a non-word char (any char other than a letter/digit/_ and, depending on the regex library, other (very rare) chars that are consider "word" chars.