Using For Each and regex

Hi all,
I'm certain this has been thoroughly explained and I'd be happy to be pointed to the relevant thread but I can't find an answer or figure it out.
I'm trying to separate text using regex as a filter and I can't figure out the correct way to do it. The simplest case is that I want to separate text like the following into different variables:

  • Text and words.
  • Text and words.
  • Text and words.
  • Text and words.
  • Text and words.
  • Text and words.
  • I don't have to use regex for this, I'm open to any method. I've done it before using For Each and LocalIndex but I don't understand it well enough to modify it—I made it copying something from JMichealTX (may his good works live forever).

    This problem comes up a lot so I'd really like to get a handle on it but I can't find anything that explains the For Each very well. The guide gives some info but leaves a lot out, imo.

    Anyway, if anyone can help, I'd really appreciate it.

    Did you know that there is a built-in way to separate the lines of a variable in keyboard maestro using the “Variable” token? I don’t think you need to separate them because they are already available as separate values using that token. Please read the largest paragraph of this page:

    https://wiki.keyboardmaestro.com/token/Variable

    1 Like

    Thank you, that was helpful. I made some progress but can't figure out how to separate based on line breaks and although I was able to display text using periods as a separator I wasn't able to set new variables that way.
    I think I'll have to give up on this solution for this project and look through the threads some more when I have a chance.
    Keyboard Maestro is such a strange mix of intuitive and completely opaque—at least to me and it often feels like you have to understand the whole thing at once to get any of it to work.

    Showing is always clearer than words.

    Can you show us:

    1. Sample input, and
    2. corresponding output ?
    1 Like

    The documentation is densely worded so maybe you missed that:

    In v11+, the separator is processed for backslashes, so you can use %Variable% <VariableName>[3]\n % to get the 3rd line.

    For example, if you set your variable local foo to the following..

    line one
    line two
    and so on

    ... then you can get the value of the second line with:

    %Variable%local foo[2]\n%

    (This is using the variable as an array, with the index starting from 1).

    1 Like

    Thank you. The Variable Token is great, I went over that page, several of the linked pages, tested /n /R, read up on those. Don't get me wrong, I'm definitely missing some key points, just not sure which.

    Here is a version that I can't get to work with the kind of text I'm trying to manipulate. My guess is that there is not actually a hard return there but I don't know what to use as a separator if that is the case. Or maybe my syntax is bad?

    Copy Tips (Testing).kmmacros (8.7 KB)

    This is the result, from left to right starting at the top left:

    You just have the slash in the wrong direction.

    %Variable%Tips[1]\n%

    2 Likes

    ahahahahaahahah omg. Thank you. Back to experimenting now that this part is working.

    1 Like

    No Problem! Depending on your needs, it could also be helpful to cycle through the lines of a variable with a "For Each" loop, and use an incrementing variable to store each line in it's own variable without using the variable indexes.

    Something like this:

    1 Like