Modify text before specified character (bold text before colon ":")

What I am trying to do: I want to modify the clipboard text to add "**" to the beginning of the selection and the position immediately left of the colon character ":".

For example:
"Authors' perspective: Julia Powles and Helen Nissenbaum stress the importance of moving beyond the first wave of accountability." becomes "Authors' perspective: Julia Powles and Helen Nissenbaum stress the importance of moving beyond the first wave of accountability."

I've tried using shell scripts and RegEx, but I'm not knowledgeable enough and keep hitting walls. Can someone please help? Thank you.

There are probably many ways to do this; here's one:

regex.kmmacros (3.2 KB)

If you have a multi-line variable, you'll want the search part to search all lines, like this:

(?m)^"(.*?):(.*)$

There are probably more efficient ways to do this; I've only got average-level regex knowledge, but it works.

The expression searches from the beginning of a line (the carat) for the double quotes followed by anything up to the first colon, and saves that as capture group 1 ($1). Then the colon, then everything else to the end of the line gets captured as $2.

For the replacement, the leading quotes go in, then the asterisk, the first capture group, the colon-space, and the second capture group.

-rob.

1 Like

Sorry, I missed the "clipboard text" part of the question. You wouldn't use a variable, just change the regex to search and replace on the clipboard directly:

Keyboard Maestro Export

-rob,

I suspect he didn't mean for the quotes to be part of the text.

Answering users' regex questions for one of our apps for years, I long ago decided it's best to answer what was asked, and then adjust as needed. If the quotes aren't there at all, it's a pretty simple adjustment:

Keyboard Maestro Export

But I guess we'll have to wait for a reply to find out.

-rob.

Thanks for your help! I was able to take what you created and finalize it for my needs. Here's what I have now:
image

Both the method with the clipboard and custom variable would work for my needs, I just want to go with whatever is more efficient. Guessing clipboard as it's one less step.

If you think there are potential efficiency gains that can still be made, please let me know. (ignore the down arrow keystroke, that's just to queue up the next line).

Bold text before colon.kmmacros (22.5 KB)