# will match the lines that contains start-text and end-text and any line in between (up until 20) .*start-text.*((?:.* ){1,20}).*end-text.* # If more than 20 lines, there won't be a match