Delete everything after pattern with RegEx

A swapping-ground for Regular Expression syntax

Delete everything after pattern with RegEx

Postby vilamonster » Thu Nov 21, 2024 2:59 am

Hello,
I am trying to remove everything after a certain pattern. The pattern is similar to S05E02 (season 05 episode 02)
The pattern could be anywhere in the filename. Usually in the middle.

I tried this: S\d{2}E\d{2}(.*)

But, I am not getting the results I want.
I apologize if this is very simple. Have tried many times and can't get it to work.

Thanks.
vilamonster
 
Posts: 2
Joined: Mon Nov 11, 2024 4:27 pm

Re: Delete everything after pattern

Postby Admin » Thu Nov 21, 2024 8:14 am

If you have a file name like:

Text1 S05E02 Text2.ext

and want to crop everything after S##E## where # is a digit, then use:

RegEx(1) [enable v2 flag]
Match: (.*)(S\d{2}E\d{2})(.*)
Replace: \1\2


Explanation:

Match: (.*)(S\d{2}E\d{2})(.*)

(.*):

This matches any sequence of characters, including none at all (greedy match).
It captures this sequence into Group 1.

(S\d{2}E\d{2}):

This matches a specific pattern:
S: The literal character S.
\d{2}: Exactly two digits.
E: The literal character E.
\d{2}: Exactly two digits.

For example, this matches strings like S01E01, S99E99, etc.
It captures this part into Group 2.

(.*):

This again matches any sequence of characters, including none at all (greedy match).
It captures this sequence into Group 3.

In essence, this regex breaks the string into three parts:

Group 1: Anything before the SXXEXX pattern.
Group 2: The SXXEXX pattern itself.
Group 3: Anything after the SXXEXX pattern.

Replace: \1\2

\1: Refers to the content captured in Group 1 (everything before SXXEXX).
\2: Refers to the content captured in Group 2 (the SXXEXX pattern itself).
The replacement string omits Group 3, effectively removing everything after the SXXEXX pattern while retaining the prefix and the matched SXXEXX pattern.

Example
Input String:
"Hello S01E01 World"

Match Breakdown:
Group 1: "Hello "
Group 2: "S01E01"
Group 3: " World"
Output (After Replacement):
"Hello S01E01"
Admin
Site Admin
 
Posts: 2641
Joined: Tue Mar 08, 2005 8:39 pm

Re: Delete everything after pattern with RegEx

Postby Admin » Thu Nov 21, 2024 8:38 am

BTW it can also be done using Replace (3) instead:

Replace: S??E?? *
With: S**E**

Replaces S??E?? followed by a space and some text, with just S**E** (the pattern itself but without following text).
? stands for a single character while * is a string of characters.
Admin
Site Admin
 
Posts: 2641
Joined: Tue Mar 08, 2005 8:39 pm

Re: Delete everything after pattern with RegEx

Postby vilamonster » Sat Nov 23, 2024 5:31 am

OH wow, this is so great!

Thanks, you have saved me a lot of work and I learned something new.

I really appreciate it. :)
vilamonster
 
Posts: 2
Joined: Mon Nov 11, 2024 4:27 pm


Return to Regular Expressions