First line of a text

Hello,

I am writing a macro to copy to the clipboard the title of a text in plain text

I am trying to figure out the regex to copy to the clipboard

  • the title of a text in plain text
  • title = first line (a misnomer meaning all before the line feed, ie the title can be very long and take up multiple lines)

Fishing around in reddit, I found the following 3 options which did not work:
(.+)\R(?s)(.+)
.+
\A.*

thank you very much

How about

(?m).*$
1 Like

works perfectly. Thank you so much !!

1 Like

I must have misunderstood something here or be imagining the wrong context. When used in KM, would .* not suffice? No?

2 Likes

yes, it does work. Thank you !

1 Like

Do you really need a regular expression ?

( The ordinary %Variable% token gives you any line of a text by its index, where the first line has index 1)

See: manual: Variable Arrays – Custom Delimiters


First line.kmmacros (2.5 KB)

3 Likes

and FWIW, if you were using an earlier version of KM (before the support of \n as a custom delimiter for variable arrays), you could use the lines collection of the For Each action:

First line (from For Each action).kmmacros (3.5 KB)

2 Likes

thank you. Very useful macro for any "for each line" situation. thank you

1 Like

Phew! I've made a few howlers here lately so I thought maybe I'd lost it. :stuck_out_tongue_winking_eye:

Season's greetings to all.

1 Like

The sed command works as well.

3 Likes

So it is sed… For anyone else besides me who does not often use sed: the -n flag is to stop the input being repeated in the output, and 1p means "print line 1".

1 Like

My personal preference is to go straight for %Variable%local_Text[1]\n%,
avoiding the need for an additional action,
but as a variation on the command line theme, there is also, of course:

head -1


First line (head command line).kmmacros (2.1 KB)

5 Likes

Ah, of course. Two classic methods.

One way and another, it's almost as if it's hard to avoid getting the first line of text! :upside_down_face: