by 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"