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
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.