# Regular Expression Trouble

**URL:** <https://www.ruby-forum.com/t/regular-expression-trouble/156395>\
**Category:** Ruby\
**Created:** [December 26, 2008, 5:41pm UTC](https://www.ruby-forum.com/t/regular-expression-trouble/156395 "2008-12-26T17:41:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [December 26, 2008, 5:41pm UTC](https://www.ruby-forum.com/t/regular-expression-trouble/156395/1 "2008-12-26T17:41:27Z")

</div>

Hey!

I’m writing a little script to rename files with weird names but common  
bits to a common format. Here’s the regular expression I use to extract  
the common data:

/._s?(\d?\d)x?e?(\d\d)._.(\w{3})$/i

It’s meant to rename TV Show files, both video and subtitles. I’m having  
trouble making it match the first group however. One of the digits  
should be optional, so I stuffed a ? sign there. However, it only  
matches one digit, leaving strings like 12x14 with captures [“2”, “14”].

I’m not quite sure if there’s anything wrong with my expression there,  
but I’d swear it’s the way to do it. Maybe you’re able to see something  
I’m looking past.

Thanks!

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [December 26, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/regular-expression-trouble/156395/2 "2008-12-26T17:56:29Z")

</div>

On Dec 26, 2008, at 10:41 AM, Chris H. wrote:

> /._s?(\d?\d)x?e?(\d\d)._.(\w{3})$/i
> 
> It’s meant to rename TV Show files, both video and subtitles. I’m  
> having  
> trouble making it match the first group however. One of the digits  
> should be optional, so I stuffed a ? sign there. However, it only  
> matches one digit, leaving strings like 12x14 with captures [“2”,  
> “14”].

Your problem is that .\* is swallowing as many characters as it can. In  
this case, it is swallowing up to and including the ‘1’. It then  
stops, because you’ve said you need at least one digit before the ‘x’,  
and that’s the second \d.

If you change it around to

/.\*(\d\d?)x/

it should work. Note that I’ve also removed the s?, as that can never  
actually be used—the .\* will swallow the ‘s’.

Regards

Dave T.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [December 26, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/regular-expression-trouble/156395/3 "2008-12-26T18:25:47Z")

</div>

Chris H. wrote:

> digits should be optional, so I stuffed a ? sign there. However, it  
> only matches one digit, leaving strings like 12x14 with captures [“2”,  
> “14”].
> 
> I’m not quite sure if there’s anything wrong with my expression there,  
> but I’d swear it’s the way to do it. Maybe you’re able to see  
> something I’m looking past.
> 
> Thanks!

.\* should probably be set to non greedy .\*?, so it doesn’t grab things  
you don’t want.

/._?s?(\d?\d)x?e?(\d\d)._?.(\w{3})$/i

What exactly does the data you’re pulling the digits and 3 letter file  
extension from look like?

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [December 26, 2008, 6:39pm UTC](https://www.ruby-forum.com/t/regular-expression-trouble/156395/4 "2008-12-26T18:39:52Z")

</div>

Howdy!

I’ve used [www.rubular.com](http://www.rubular.com) when fiddling around with Ruby regular  
expressions.  
It will show matches in real time as you type on an arbitrary text. It  
helped me a lot.

```
Magnus

```

* * *

Från: removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid]  
Skickat: den 26 december 2008 17:41  
Till: ruby-talk ML  
Ämne: Regular Expression Trouble

Hey!

I’m writing a little script to rename files with weird names but common  
bits to a common format. Here’s the regular expression I use to extract  
the common data:

/._s?(\d?\d)x?e?(\d\d)._.(\w{3})$/i

It’s meant to rename TV Show files, both video and subtitles. I’m having  
trouble making it match the first group however. One of the digits  
should be optional, so I stuffed a ? sign there. However, it only  
matches one digit, leaving strings like 12x14 with captures [“2”, “14”].

I’m not quite sure if there’s anything wrong with my expression there,  
but I’d swear it’s the way to do it. Maybe you’re able to see something  
I’m looking past.

Thanks!

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [December 29, 2008, 10:59am UTC](https://www.ruby-forum.com/t/regular-expression-trouble/156395/5 "2008-12-29T10:59:58Z")

</div>

Tim G. wrote:

> .\* should probably be set to non greedy .\*?, so it doesn’t grab things  
> you don’t want.
> 
> /._?s?(\d?\d)x?e?(\d\d)._?.(\w{3})$/i

This one actually worked. I tried non-greedies all over, but there.  
Thanks!!

And thanks also for the hint at rubular. It’s awesome.
