ronald
December 24, 2024, 7:19pm
1
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
ronald
December 24, 2024, 8:07pm
3
works perfectly. Thank you so much !!
1 Like
kevinb
December 25, 2024, 1:15am
4
I must have misunderstood something here or be imagining the wrong context. When used in KM, would .*
not suffice? No?
2 Likes
ronald
December 25, 2024, 4:44am
5
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
ronald
December 25, 2024, 11:15am
8
thank you. Very useful macro for any "for each line" situation. thank you
1 Like
kevinb
December 25, 2024, 12:04pm
9
Phew! I've made a few howlers here lately so I thought maybe I'd lost it.
Season's greetings to all.
1 Like
kcwhat
December 26, 2024, 3:54am
10
The sed command works as well.
3 Likes
kevinb
December 26, 2024, 1:19pm
11
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
kevinb
December 26, 2024, 10:20pm
13
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!