Regular Expression Trouble

C

Chris Hoeppner

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!
 
D

Dave Thomas

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

It's meant to rename TV Show files, both video and subtitles. I'm =20
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", =20
"14"].

Your problem is that .* is swallowing as many characters as it can. In =20=

this case, it is swallowing up to and including the '1'. It then =20
stops, because you've said you need at least one digit before the 'x', =20=

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 =20=

actually be used=97the .* will swallow the 's'.


Regards


Dave Thomas=
 
T

Tim Greer

Chris said:
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!

..* 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?
 
M

Magnus Erickson

Howdy!

I've used www.rubular.com when fiddling around with Ruby regular expression=
s.
It will show matches in real time as you type on an arbitrary text. It help=
ed me a lot.

Magnus

________________________________________
Fr=E5n: (e-mail address removed) [[email protected]]
Skickat: den 26 december 2008 17:41
Till: ruby-talk ML
=C4mne: 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!
 
C

Chris Hoeppner

Tim said:
.* 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top