[regex] Regular Expression to select everything before and up to a particular text

I am trying to use a regular expression to find a part of a string and select everything up to that string. So for example if my string is this/is/just.some/test.txt/some/other, I want to search for .txt and when I find it select everything before and up to .txt.

This question is related to regex

The answer is


Up to and including txt you would need to change your regex like so:

^(.*?\\.txt)

This matches everything up to ".txt" (without including it):

^.*(?=(\.txt))


You could just do ...

(.*?)\.txt

tested here..


((\n.*){0,3})(.*)\W*\.txt

This will select all the content before the particular word ".txt" including any context in different lines up to 3 lines