Regex - how to match previous token multiple times v0

The focused string is :
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16;1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16;1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16;1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16;

I want to match the first 16 numbers. In regex101, I can use the Regex:
((\d{2}|\d)(\,\d{2}|\,\d){15}(\;))
the (\,\d{2}|\,\d){15} means match (\,\d{2}|\,\d) 15 times. But this token doesn't work in KM.

In KM, If I use
((\d{2}|\d)(\,\d{2}|\,\d){15}(\;))
the (\,\d{2}|\,\d){15} only match the 15th number.

I cannot find the correct way to do this, So I have to use this:

((\d{2}|\d)(,\d{2}|,\d)(,\d{2}|,\d)(,\d{2}|,\d)(,\d{2}|,\d)(,\d{2}|,\d)(,\d{2}|,\d)(,\d{2}|,\d)(,\d{2}|,\d)(,\d{2}|,\d)(,\d{2}|,\d)(,\d{2}|,\d)(,\d{2}|,\d)(,\d{2}|,\d)(,\d{2}|,\d)(,\d{2}|,\d)(\;))

(note that in this post, it's (\,\d{2}|,\d) actually, but This forum page show it in (,\d{2}|,\d). The first '\' was hidden. Don't know why!!!!!!)

This Regex works, but not elegant and smart.

So I want to know how to use Regex to match previous token multiple times in KM.
(I mean how to do (\,\d{2}|,\d)){15} in KM?)

And another question is that you see the focused string is that combined with four 16-numbers-group. Is there a way to match them one group by one group at a time?

Here is the KMM:
Regex Question - how to match previous token multiple times v0_Backup_20220827_2216_v0.kmmacros (4.4 KB)

Lots of things to say about this.

First,

That is the correct behavior if what you are extracting is the third capture group. The third capture group is whatever was captured by the third set of parentheses, which is the set just before the {15}. The text last captured by that is ,16. If you want the full set of 16 numbers, look for either the entire match (what Keyboard Maestro calls "All") or the first capture group.

Second, I would suggest a simpler regex to capture the set of 16 comma-separated numbers

\d{1,2}(,\d{1,2}){15};

The \d{1,2} parts mean "one or two digits," just like your \d{2}|\d but shorter and easier to read. Also, you don't need to worry about putting a backslash in front of the comma that separates the numbers or the semicolon—neither of them have special meaning.

Finally, while you may have good reason to use regular expressions to do this work, consider just replacing semicolons with linefeeds. Then you can loop through each 16-number set and do whatever you want with them. For example, this macro

Sixteen-Number Sets.kmmacros (4.0 KB)

Show macro image

will put up 4 windows on your screen, each with one of the 16-number lists.

Of course, whether any of this works depends on how well-behaved your input is.

3 Likes

and you can also reference the substrings directly, without any loops or regular expressions.

(Using custom delimiters and one-based indices)

Splitting on semicolon and comma.kmmacros (3.5 KB)


3 Likes

I see. So
(,\d{2}|,\d){15}
works.
But I need to make it
((,\d{2}|,\d){15} )

That's the difference between with or without () in KM.
(,\d{2}|,\d){15}
-- means the 15th match
((,\d{2}|,\d){15} )
-- means all the 15 matched case

Many thanks!

Question Regex - how to match previous token multiple times v0 -- drdrang's answer_Backup_20220828_0704_v0.kmmacros (5.2 KB)

I've been using KM for some years, never thought I can use 'custom delimiters' in this way.
Very elegant and Inspiring.
Thanks!

1 Like