Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Bookmark Two Last Non-Empty Lines Before a Specific Line?

I have a list like following:

Word1
Word 2
Word 3$
Word45

fghjyh

Q1

/////////////////////////

Commando1

Commando 43
Doctor##

44GGTT45

TTTTTT

QQQWW22
/////////////////////////

Now I want to bookmark two last non-empty lines before ///////////////////////// lines.
regex must bookmark following lines:

fghjyh
Q1
TTTTTT
QQQWW22

I tried following regex but not working:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

^\h*\S.*(?=\R+/{24})(?=\R+\h*\S.*(?=\R+/{24}))

where is problem in my regex?

>Solution :

First of all, positive lookaheads that are located in immediate succession in the pattern all trigger at the same location in the string, and if they require different text to appear to the right, there will never be a match. Your (?=\R+/{24})(?=\R+\h*\S.*(?=\R+/{24})) lookaheads mutually exclude each other. If you remove (?=\R+/{24}) there will be a match, but it will only be a single line as (?=\R+\h*\S.*(?=\R+/{24}) requires a whole line before the line with 24 slashes. Hence, you need to make one line pattern optional in it.

So the solution may look like

^\h*\S.*(?=\R+(?:\h*\S.*\R+)?/{24})

See the regex demo.

Details:

  • ^ – start of a line
  • \h* – zero or more horizontal whitespaces
  • \S.* – a non-blank line (a non-whitespace + zero or more chars other than line break chars as many as possible)
  • (?=\R+(?:\h*\S.*\R+)?/{24}) – a positive lookahead that matches a location that is immediately followed with
    • \R+ – one or more line breaks
    • (?:\h*\S.*\R+)? – an optional sequence of zero or more horizontal whitespaces + a non-whitespace + one or more line break sequences
    • /{24} – 24 slashes.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading