Nth Line of a Text

I need a regular expression to match line 3 in some text. I have tried this:

\A(?:^.$\n){3}\K^.$

But that didn't work. Any ideas how I can match line 3?

See:

XY problem - Wikipedia

and

Tip: How Do I Get The Best Answer in the Shortest Time? - Tips & Tutorials - Keyboard Maestro Discourse

1 Like

Have a look at this macro that extracts any specified line from a list:

3 Likes

Keyboard Maestro directly supports:

  • custom delimiters
  • indexed segments of variables

So, without adding a regular expression as a further problem:

Third line.kmmacros (3.4 KB)

2 Likes

It is a shame one of those delimiters cannot be directly a new line \n

(Although @peternlewis has hinted that this might be implemented one day...)

As a variation of of your solution I have been using the below a lot recently as it forces \n to work as a delimiter. It allows me to see at a glance that I am getting the nth line from a Variable).

EXAMPLE Get Line 3.kmmacros (3.7 KB)

EDIT - And it's worth pointing out that in this Keyboard Maestro native method the number in the square brackets can also be a Variable:

So, if a Variable LOCAL__Number was set to "4" it would get line 4.

%Variable%LOCAL__Variable(LOCAL__Number]\n%

Or in @ComplexPoint's version

%Variable %local_Delimited[LOCAL__Number]~,~%

2 Likes

If we're playing, here's one using shell script actions that will also return +ve and -ve (counting from the end) lines:

Get Line.kmmacros (6.3 KB)

Image

3 Likes

Hey Ross,

This question has come up a number of times on the forum. Here's one example:

Feature Request: Filter or Token: "nth Line"

Your regular expression above is not correct for the task at hand.

Here's one that works.

-Chris


Capture 3rd Line of Textual Data v1.00.kmmacros (6.4 KB)
Keyboard Maestro Export

4 Likes

Many thanks for your excellent help as usual.

2 Likes