RegEx – Extract Several Matches/Groups

Hi guys,

I have a regex-string which works in regex 101 but not in Keyboard Maestro.

See here: regex101: build, test, and debug regex

I tried the suggestions in MACRO: Get List of RegEx Capture Group of Multiple Matches but can't get it to work.

Here is the current status:

Which results in:

The goal is to get the specific parameters in different variables so I can automatically paste them into other code and forms.

Does anyone know what I am doing wrong?
Maybe the regex-ninja @JMichaelTX who provided the awesome template?

Thanks

Julian

You havent’t defined the other variables (for your other capture groups). You’ve defined only the first capture group. Provide names for the variables corresponding to capture groups 2-6.

Hey Julian,

When posting regex requests please provide the following:

  1. A turnkey macro demonstrating the test case as simply as possible.

  2. The desired outcome in detail.

OR:

  1. One or more data samples as preformatted ("fenced") text, so it is correctly displayed by the forum and not rendered.

  2. The desired outcome in detail.

Text parsing is complicated enough without having to try to read the user's mind and decipher what they're trying to do.

The easier you make it for people to test your work the more likely you'll get quality help in expeditious fashion.

-Chris

Thanks for your input. Since the number of variables is not fixed (4+) it is not possible to define all of them. As I understood the used makro template it will extract as many variables as there are regex matches. It seems to find several once (the output shows several MatchStrings) but only the first has a real value.

Thats what got me confused :slight_smile:

Hey Chris,

thanks for explaining what I should provide. Makes 100% sense.
I put all detailed information inside the makro example, so everyone can see the original template I used and how everything works. It also includes easy color coding.

TEST Infusion Form Extract.kmmacros|attachment (46.5 KB)

I'm posting this alternative, not because I think it's a better solution to your specific application, but just to illustrate to anyone interested that regex can be used to insert delimiters around sought-for text within a document, storing the result in a variable. The variable that's created is now an array in which every other element (separated by a delimiter) is an instance of the sought-for text. Depending on whether the document starts with an instance of the sought-for text, or the document starts with some other text, the sought-for instances will be the odd-numbered or the even-numbered elements in the resulting array.

In this example, I'm assuming that I have some text whose contents I do not know, but I do know that the text will contain some number of five-digit serial numbers, each preceded by the string, "Serial Number: ". That's the pattern in this example; your pattern can be anything. Also, in this example, I chose to use <delimiter> as my inserted delimiter, because <delimiter> is almost certainly not going to accidentally appear in my unknown document. You may set any string as your chosen delimiter, however.

The macro initializes and then appends two variables (OddMatches and EvenMatches) with every other element that occurs between <delimiter>. One of those variables will contain the "trash" (non sought-for text), and the other will contain the sought-for incidences. After loading these variables, the macro then prepends each variable with the number of useful elements it contains. The nice thing about storing these results in delimited variables serving as arrays is that the elements of the arrays are then fully accessible and manipulable for whatever purpose. For instance %Variable%EvenMatches[1]<delimiter>% gives me the number of sought-for matches that were found. This number is useful in setting the limits for loops, among other reasons. If I want, say, the second match of the sought-for text, it's simply %Variable%EvenMatches[3]<delimiter>%, and so on.

Extracting Capture Groups Macro (v9.2)

Extracting Capture Groups.kmmacros (7.4 KB)

By the bye, the creation of odd and even variables here is only for illustrative purposes. It is not necessary to do this, of course. More directly, an Until loop could iterate through the full delimited array (SampleTextDelimited) and append a new variable (say, Matches) with only the elements that contain "Serial Number: ".

1 Like

Hey @WinWinWin,

This is too complicated to mess with in my opinion.

I assume you're getting your data from the source of a web page, but you ought to tell use how you're getting it.

If this is from a live page in Safari or a Chrome variant I'd use JavaScript in a Execute a JavaScript in Front Browser action to get the values.

That's generally programmatically exact and significantly better than parsing text.

If I have to resort to brute-force parsing I'd much rather use a series of relatively simple find and/or find/replace actions to get the job done, instead of relying on a complicated and fragile monolithic regex.

Here I've used Perl to make the job much more compact and readable (for me), but it can all be done with Keyboard Maestro native actions.

Infusion Form Extract with Perl v1.00 @ccstone.kmmacros (9.2 KB)

-Chris

Hey @ccstone,

Thanks for this great alternative. I used Regex for one reason only: It's the only thing I knew which might do the trick. I actually paid someone to build the regex. So your solution is awesome and also worth paying for. Thanks.

I get the code out of a crm-tool. So it gets copied into my System Clipboard.

After extracting the values I would like to paste the individual values one after another into another webform. So I need each line as a variable and then a function to paste on, switch to the next field, paste #2, switch, paste #3 etc.

Is there a way to get them easily into separate variables or built a function through which I can paste them line by line?

This help is awesome. Is there a way to hire you as a freelancer for potential macros in the future?

Julian

Hey Julian,

Yes.

This macro is the same as the previous one, except it reverses the output and then iterates through and puts each line on the clipboard for later use with Keyboard Maestro's clipboard history.

Infusion Form Extract with Perl v1.01 @ccstone.kmmacros (11 KB)

From here you need to construct a macro that:

  • Pastes the System Clipboard.
  • Deletes past clipboard 0

Then you can use macro 1 to collate your data and macro 2 paste into your web-form.

-Chris