Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to select everything before and up to a particular text

Tags:

regex

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.

like image 803
peztherez Avatar asked Aug 23 '13 23:08

peztherez


People also ask

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

How do you match anything up until this sequence of characters in regular expression?

Take this regular expression: /^[^abc]/ . This will match any single character at the beginning of a string, except a, b, or *c. If you add a * after it – /^[^abc]*/ – the regular expression will continue to add each subsequent character to the result, until it meets either an a , or b , or c .

What does \b mean in regular expressions?

Matches only at the end of the string. Most of the standard escapes supported by Python string literals are also accepted by the regular expression parser: \a \b \f \n \N \r \t \u \U \v \x \\ (Note that \b is used to represent word boundaries, and means “backspace” only inside character classes.)

What does (? I do in regex?

E.g. (? i-sm) turns on case insensitivity, and turns off both single-line mode and multi-line mode.


1 Answers

After executing the below regex, your answer is in the first capture.

/^(.*?)\.txt/ 
like image 125
kikuchiyo Avatar answered Oct 03 '22 01:10

kikuchiyo