RegEx Finds Match, ForEach Loop Does Not

Hi everyone!
Advance note: I am still on KM Version 9.

My macro successfully manages to read a rather large (130 kB) text file into the variable TXTtoGPX_Input. I want to scan the entire text with RegEx for certain patterns and process each pattern accordingly.

The command Search Variable “TXTtoGPX_Input” Using Regular Expression (ignoring case)" works and finds at least the first match, I can see it in the editor window.

But as soon as I put this into a For Each loop, nothing happens. I don't even get an error message, but I guess the RegEx fails for some reason, but it did work when not within the For Each loop. The variable TXTtoGPX_Match simply counts the matched loops, but it stays zero...

Where did I go wrong?

Here is the For Each loop in question:

  • [image]For Each Item in the Collection Execute Actions
  • For each TXTtoGPX_Match in these collections:
  • [image]The substrings in Variable “TXTtoGPX_Input”

Execute the Following Actions:

  • [image]Search Variable “TXTtoGPX_Input” Using Regular Expression (ignoring case)

Search for “((\s)([A-Z]{0,}[0-9]+|)(\s)([A-Za-zäüöß+,_-0-9. ()]+)(\s)([0-9,]{1,}+|)(\s)([4-5]0-9[0-9]{1,})(\s+|,.|,|, |./.)(0-9[0-9]{1,}))”
And capture to:

0. *Ignored*
1. *Ignored*
2. *Ignored*
3. LocalSchlagnummer
4. *Ignored*
5. LocalSchlagname
6. *Ignored*
7. LocalSatzgroesse
8. *Ignored*
9. LocalGPSNdezimal
10. *Ignored*
11. *Ignored*
12. LocalGPSEdezimal
13. *Ignored*Stop macro and notify on failure.
  • [image]Set Variable “TXTtoGPX_Match” To Calculation “TXTtoGPX_Match+1”

TXTtoGPX_Match+1

--- end quote

And this is an excerpt of the text file in question - regex101 matches everything just fine (there are like 30 matches in the entire text). Note that the text file is a tab-separated UTF format text. The areas with the coordinates are supposed to get fetched by the RegEx.

Textfile-Quote:
3. Vorjahr
Art:
Menge/ha:
" ( ) nein "( ) ja
( ) nein" Bor x
2 Am Schlittweg2 7,0 49.416606 8.435426 Soja "( x ) Keine
( ) Leguminosen
( ) Nichtleguminosen
( ) abgefroren
( ) Einarbeitung
( ) Herbst
( ) Frühjahr
" Kartoffeln "( x ) ja
( ) nein" Kg N / ha: "( ) ja

  1. Vorjahr
    Art:
    Menge/ha:
  2. Vorjahr
    Art:
    Menge /ha:
  3. Vorjahr
    Art:
    Menge/ha:
    " ( ) nein "( ) ja
    ( ) nein" Bor x
    3 Am Schlittweg4 3 49.415795 8.437477 Soja "( x ) Keine
    ( ) Leguminosen
    ( ) Nichtleguminosen
    ( ) abgefroren
    ( ) Einarbeitung
    ( ) Herbst
    ( ) Frühjahr
    " Möhren "( x ) ja
    ( ) nein" Kg N / ha: "( ) ja
  4. Vorjahr
    Art:
    Menge/ha:
  5. Vorjahr
    Art:
    Menge /ha:
  6. Vorjahr
    Art:
    Menge/ha:
    " ( ) nein "( ) ja
    ( ) nein" Bor x
    3 Am Schlittweg5 4,62 49.415795 8.437477 Soja "( x ) Keine
    ( ) Leguminosen
    ( ) Nichtleguminosen
    ( ) abgefroren
    ( ) Einarbeitung
    ( ) Herbst
    ( ) Frühjahr

Hey Chris,

You need to post an actual test-case macro that demonstrates the problem.

People need to see and test your work – not reinvent the wheel imperfectly according to an explanation and thus wasting everyone's time.

BTW – any special reason you're still using Keyboard Maestro 9? As I recall there are some improvements related to matched strings in a For-Each action.

Take Care,
Chris

(Keyboard Maestro Moderator)