Regular expression question

Working with a renaming application that supports regular expression renaming.

I really want to learn regular expressions, because I often find myself wanting to use them, but so far I’m still pretty clueless.

The files I’m renaming are episodes of TV shows. Often the name will be “blahblah S03e02” or “Blah blah s01e22” I want to the application to find instances of the letter e or s in lowercase directly next to whatever digits, and change that lowercase e/s to an uppercase E/S.

My attempt thus far has been
string
e[0-9] - successfully locates

replace with
E - no, loses the digits entirely
E[0-9] - no, becomes E[0-9]

So how do i use regular expressions to say “find these lowercase letters by location, (which is next to any digits) and then change the lowercase to uppercase but don’t mess with the digits that you found them next to”?

You need to capture the digits in the pattern, and then reference the capture group in the replacement. You should also be able to use \d as a shortcut for digit characters instead of the character class [0-9].

To capture part of a pattern, use parentheses.

So your pattern should look something like



s(\d+)e(\d+)


In a lot of regex flavors, the capture groups will go into variables called $1, $2, $3, and so on. Assuming your regex system does that, your replacement string would be



S$1E$2


And in the case that some files have inconsistent cases between the ‘e’ and the ‘s’, you could catch those, too, with a slightly more general expression:



[sS](\d+)[eE](\d+)


with the same replacement string as in friedo’s post.

Or you could do a simpler find and replace operation using
<space>s0 replace with <space>S0
<space>s1 replace with <space>S1

Then
e0 replace with E0
e1 replace with E1
e2 replace with E2

just saying.

In case you haven’t figured it out, regexes are challenging. And regex engines come in dozens of not-quite-the-same dialects.

Regexes with replace can be very dangerous because there is no undo. It’s real easy to destroy a lot of info that way.

Before you go loosing your regex on a file folder you want to perfect it on a text file that contains the names you intend to process.

Here’s some useful documentation on practical regex use: xkcd: Regular Expressions, xkcd: Perl Problems, and xkcd: Regex Golf. Don’t forget to read the mouseover text; it’s like footnotes. *Very *important for getting the details right.

I would hope that OP’s renaming program has an option to simulate the renaming. That way she can use the exact same RegEx engine.