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:
^\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.