Search for Multiple Line Regular Expression

I can’t find out how to search in KM for multiple line regex. For Example if i need to find everything between words “begin” and “end”:

begin
line 1
line 2
line 3
end

Thank you!

1 Like

If you just want to catch everything between “begin” and “end”, this should do:

_[example] Regex Multiple Lines.kmmacros (2.6 KB)

A very good starting point is the KM Wiki article about Regular Expressions. It explains the basics, gives you examples, and – if you scroll down – many very useful links, also to existing Forum topics.

It also explains most of the expressions used in the macro.


If you want to retrieve specific information out of several lines, have a look at this topic:

4 Likes

Heh, I thought I knew regular expressions pretty well, but I never knew about the \R character. Thanks for that @Tom!

Keyboard Maestro > Help > ICU Regular Expression Reference :wink:

2 Likes

I know, that is what I did to look it up - I just had never actually seen it, which is a shame as its an awesome one to use!

Seems to work also in Perl. It is listed in the Camel Book (4th Ed., p.197 ) as “Match any linebreak grapheme”, further it says:

The \R matches either a CARRIAGE RETURN followed by a LINE FEED (possessively), or else any one vertical whitespace character. It is equivalent to (?>\r\n|\v). The possessive group means that “\r\n” =~ /\R\n/ can never match; once it’s seen the two-character CRLF, it will never later change that to just the CARRIAGE RETURN alone, even if something later in the pattern needs the LINE FEED for the overall pattern to succeed.

But the ICU implementation may be (a bit) different, anyway.

2 Likes