I have this string
test = "total abc total foo total anything here totalntotal totalntotalnstartnotal abc total foo total anything here totalnotal abc total foo total anything here totalnstartnotal abc total foo total anything here totaln"
How would I go on about matching from the last occurance of start
to the end of the line?
I tried to do this with a negative lookahead but I would always get the first occurance:
(?!$)\nstart[sS]*?$
Expecting match to be characters: 164-219
Advertisement
Answer
You can use
(?ms)^start$(?!.*^start$).*
See the regex demo.
Details:
(?ms)
– the.
matches newlines now and the^
/$
anchors now match start/end of any line respectively^
– start of a linestart
– a fixed word$
– end of a line(?!.*^start$)
– a negative lookahead that fails the match if there are any zero or more chars as many as possible followed withstart
as a whole line.*
– the rest of the string.