How to Extract Each Text Between Square Brackets to a Single Variable?

For example, As the picture shows below:
image

I've been trying to get this:
image

Tried to use regex \[(.*?)\] to extract text between square brackets. (Reference: Regular expression to extract text between square brackets)

But I don't know what to do next. Anyone could help? Many thanks!

Happy Lunar New Year's Eve! :laughing::laughing:
Here's the relevant macro if it's needed😉
Keyboard Maestro Actions.kmactions (783 B)

There are many ways of doing this.

Others will show you regex-only solutions, which may require a slightly more complex regex.

If we embed it in an Execute Script action, a more basic regex may be enough.

  • All the lines you want to keep orginally begin with [,
  • and other lines can be ignored.
  • In the retained lines, only the material up to ] is of interest.

terms between brackets.kmmacros (19.9 KB)

JS Source
Application('Keyboard Maestro Engine')
    .getvariable('sourceText')

    // Each line of interest starts with '['
    .replace(/\[/g, '\n')

    // What lines do we have now ?
    .split('\n')

    // Noise pruned out, and signal retained:
    .flatMap(
        s => s.includes(']') ? (
            
            // Lines with `]` retained up to that point,
            [s.split(']')[0]]

            // and other lines discarded.
        ) : []
    )
    .join('\n');
1 Like

@ComplexPoint
It works! Inspire me to learn some Javascript. Thanks!
Can't understand the script tho :joy:

1 Like

.flatMap lets us combine filtering things out with mapping each part of a list to a transformed version.

We wrap the parts we want to keep in [ ... ]

and for the bits we want to drop we just return the empty list []

.flatMap flattens out the intermediate result:

[[],['bear'],['can dance'],[],['well'],[],['They'],[],['sing'],[]]

which makes the empty lists vanish, leaving us with:

['bear', 'can dance', 'well', 'They', 'sing']

Eat well for 除夕, and have fun over 春节 !

Hahaha... Thank you so much!
Happy :cow: year! :tada::tada::firecracker::firecracker:

Happy 牛 year !

The structure of the JS is essentially this, in terms of non-script KM actions:

terms between brackets (KM Actions).kmmacros (22.1 KB)

2 Likes

@ComplexPoint You sure knew I didn't understand your script. :joy:
Now I got it. Thank you! :grin:

Your RegEx is very close.
The solution needs only a very simple RegEx, and does NOT require any complicated script. It can all be done with KM non-script Actions.

Example Output

image

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Build List from Text in Brackets [Example]

-~~~ VER: 1.0    2021-02-11 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Build List from Text in Brackets [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Build List from Text in Brackets [Example]

HOW TO USE

  1. First, make sure you have followed instructions in the Macro Setup below.
  2. Change the Text in the first Action to your source text.
  • The text to be extracted must be in square brackets.
  1. Trigger this macro.

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??

Make These Changes to this Macro

  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magenta color)
    • SET Source String
      • The text to be extracted must be in square brackets.

REQUIRES:

  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.11.6 (El Capitan)+

TAGS: @Regex @Extract @Strings @Example

USER SETTINGS:

  • Any Action in magenta color is designed to be changed by end-user

1 Like

@JMichaelTX Thank you!Search for substring that match a Regular Expression is cool!Now I understand the usage between these two:
image

image

2 Likes