views:

23

answers:

1
sed -e 's/ *-\{0,1\},\{0,1\} *[Pp][Aa]\{0,1\}[Rr]\{0,1\}[Tt].\{0,1\} *\([0-9]\{1,\}\) *$/ (\1)/'

I'm piping titles through this from an iTunes AppleScript. This should convert any title with a Part designation at the end to a parenthetical format

For instance, Best of Both Worlds, Part 2 becomes Best of Both Worlds (2).

The trouble rears its head when a space is omitted between Part and the digit(s).

  • If it's a single digit, it works fine. Blah part2 becomes Blah (2)
  • More than one digit, and the first digit gets cut off: Blah pt123 becomes Blah (23)

I freely admit that I'm no RegEx expert, but I dug around both in the book "Mastering Regular Expressions" and searched google without luck. Any idea what's going awry here? Thanks!

+1  A: 

The '.\{0,1\}' matches the 1.

Replace the . with [^0-9].

Jonathan Leffler
Oh! Duh. Except I actually do only want to match a period, so I should replace the `.` with `[.]`. Thanks for the kick in the head!
bobtiki
@bobtiki: Instead of `[.]` you can use `\.`
Dennis Williamson