Regex search works intermittently

I'm totally stumped, any help would be appreciated. I'm using regex to save the heading (first 4 lines) of some plain text notes.

The variable projecttext in this case is:

#p_test #current

202002212156 quis nostrud
===

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ...

Sometimes this works, sometimes it doesn't. I can't force it to fail or succeed reliably. Sometimes I can fix it by restarting the engine, sometimes not. Does anyone have any ideas why this might be happening?

Try this Regex:
^(?:.*\R){4}

If this fails for any case, please post the EXACT source text for which it failed.

Let us know if this works for you.

Should the 4 be a 3? I only see 3 newlines in the original search pattern.

Nope. OP requested 4 lines, and that is what my Regex does.
You can test it at regex101.com.

Just a thought but a line that looks blank may contain white space so I would rephrase the capture to avoid that.

I think the ".*" will capture white space and everything else on the line.
\R will capture any form of end of line character(s).
^(?:.*\R){4}

As I said above, if you, or anyone, has any doubts, you can test it a regex101.com.

My guess is that @JMichaelTX is right - clipboards can often have linefeed or return line endings (and rarely return-linefeed endings), so replacing your use of \n (linefeed) with \R (which matches any of the line endings) should resolve your issue.

I generally prefer your use of \A over ^, since \A always means the start of the entire string whereas ^ can alternatively mean the start of a line within the string - but it doesn't by default so it's largely academic. Also, your regex:

\A.*\R.*\R.*\R.*

will avoid capturing the trailing line ending, so is slightly different to @JMichaelTX which captures the fourth line ending. Which behaviour you want is up to you.

In all this nobody mentioned capturing groups explicitly.

The OP’s screen display showed there weren’t any - both because of the lack of a ( and ) pair and because the action didn’t have the fields which enable you to say which variables get the result of each of the capturing groups.

I am sorry to disappoint everyone but the error has not returned since plaguing me for a day. I'm still using my original regex and there doesn't seem to be any issue... I'll try your ideas if it comes up again. Thanks all

There is a “0” capture group that is implicit that captures the entire match, which is what the OP is using to capture the search of the entire regex.

1 Like