How can I find next set of brackets and highlight for replacement?

Hi. I have hundreds of template sentences to complete. the format uses braces to identify areas I need to replace with my text. Here is an example:

[PLANET] in [SIGN] in [HOUSE] ([PLANET] has [x] Votes on BOD) [PLANET] Rules [HOUSES]
To express [his/her] authentic “Big S” Self (Sun’s Board of Directors), [he/she] needs to [PLANET BOD keywords] in a manner that is [sign keywords] ([SIGN]) WITH [house ruled keywords] ([PLANET] Rules [HOUSE]). [He/She] encounters these needs particularly IN [house occupied keywords] ([PLANET] in [HOUSE]).

I use Scrivener as my editor, and have enormous template files made up of scores of sentences such as the example above that I am working with. I would like to type a hotkey that that moves my cursor to the next bracket, selects the bracket and all text up to and including the next bracket. I will then type the text I wish as the replacement and hotkey to the next bracket. I have a regex I think will work: “\[(.*?)\]” but I don’t know how to use it in keyboard maestro. This is a little more advanced for me and I could use some help. As you can see from this one example, I have a ton of replacements to type :-). Any help would be appreciated. Thanks.

If Scrivener supports searching for regular expressions, then you simply need to search for \[(.*?)\] as a regular expression and press Command-G (Find Again) to do what you want.

If Scrivener does not support searching for regular expressions, then you simply need to switch to BBEdit and use it instead, and then search for \[(.*?)\] as a regular expression and …

peternlewis, wow. Yes, Scrivener supports regex (I’ve used it for years and I didn’t know that until today :-). Your approach works perfectly! This is transformational for me. Thanks.

I am probably not understanding Peter's approach, but it seems to me that you would need to use this RegEx pattern:

You need to escape the square brackets [ ] to search as literals.

and then replace all with the desired text.

Of course, you would need to repeat this for each placeholder.

In BBEdit this would look like:

I think he was just feeding back to me the regex I put in my post. Yes, the backslashes are needed. With them, the regexp works perfectly. And, it is super fast :-). Thanks.

1 Like

Great. Glad it’s working for you.

BTW, if you have a list of placeholders and replacement text that you often use, it would not be too hard to build that into a KM macro. If you are interested let me know, and I’ll provide more details.

You could also use KM to get the list of placeholders, and then prompt you for the replacement text.

1 Like

Yes, you’re quite right, the [] need backslashes in the regex. Unfortunately, the forum does not display the backslashes if you’re not careful (as @edr wasn’t), and if you’re not careful (as I wasn’t) you don’t notice their absence.

The rounded brackets are actually optional in that regex, just \[.*?\] will work fine.

1 Like