# Regular expression question

**URL:** <https://boards.straightdope.com/t/regular-expression-question/769936>\
**Category:** Factual Questions\
**Created:** [October 28, 2016, 2:54pm UTC](https://boards.straightdope.com/t/regular-expression-question/769936 "2016-10-28T14:54:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/stoid/32/272_2.png) [@Stoid](https://boards.straightdope.com/u/Stoid)\
**Post date:** [October 28, 2016, 2:54pm UTC](https://boards.straightdope.com/t/regular-expression-question/769936/1 "2016-10-28T14:54:41Z")

</div>

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”?

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [October 28, 2016, 4:07pm UTC](https://boards.straightdope.com/t/regular-expression-question/769936/2 "2016-10-28T16:07:22Z")

</div>

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

```auto

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

```auto

S$1E$2

```

---

<div class="post-metadata">

**Author:** ![Pasta](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@Pasta](https://boards.straightdope.com/u/Pasta)\
**Post date:** [October 28, 2016, 4:35pm UTC](https://boards.straightdope.com/t/regular-expression-question/769936/3 "2016-10-28T16:35:47Z")

</div>

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:

```auto

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

```

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

---

<div class="post-metadata">

**Author:** ![curious11](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@curious11](https://boards.straightdope.com/u/curious11)\
**Post date:** [October 28, 2016, 10:28pm UTC](https://boards.straightdope.com/t/regular-expression-question/769936/4 "2016-10-28T22:28:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)\
**Post date:** [October 29, 2016, 3:32pm UTC](https://boards.straightdope.com/t/regular-expression-question/769936/5 "2016-10-29T15:32:55Z")

</div>

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](https://www.xkcd.com/208/), [xkcd: Perl Problems](https://www.xkcd.com/1171/), and [xkcd: Regex Golf](http://xkcd.com/1313/). Don’t forget to read the mouseover text; it’s like footnotes. \*Very \*important for getting the details right.

---

<div class="post-metadata">

**Author:** ![BigT](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bigt/32/12044_2.png) [@BigT](https://boards.straightdope.com/u/BigT)\
**Post date:** [October 29, 2016, 7:20pm UTC](https://boards.straightdope.com/t/regular-expression-question/769936/6 "2016-10-29T19:20:47Z")

</div>

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.
