View Single Post
Old 12-25-2010, 06:28 PM   #2
arberg
Junior Member
arberg began at the beginning.
 
Posts: 1
Karma: 10
Join Date: Dec 2010
Device: Sony PRC-650
I think it might be more helpful for people to learn that there exists tools which can help us humans create regular expressions. This is one such tool:

http://gskinner.com/RegExr/

It's pretty good, unfortunately it doesn't show which group matches which strings, and it doesn't acknowledge certain special python/calibre rules (such as []] and [-]) but it does allow you to write your filename you want to match, and then type and see how far your current expression matches the string.

Here's my expression (to get back on topic :

^(?P<author>[^-]+)(\s*-\s*(\[?(?P<series>[^-0-9]+)\s*(?P<series_index>[0-9.]+)?]?)?)?.*?-\s*(?P<title>[^\]{[()]+\w)

The expression expects the filename to start with Author and end with Title (possibly followed by garbage in parentheses). It also optionally matches series, and series index in the middle. It requires the title to end in an alpha-numeric character, and it does not allow the title to contain any kinds of parentheses (anything following a parenthesis will be discarded)

It matches the following examples filenames
Author Harris - Kingdom come
Author Harris - Kingdom come (v1.0)
Author Harris - Kingdom series - The Very Magical Kingdom (v1.0)
Author Harris - Kingdom series 14.5 - The Very Magical Kingdom (v1.0)
Author Harris - [Kingdom series 14.5] - The Very Magical Kingdom (v1.0)
Author Harris - [Kingdom series 14.5] [another useless string]- The Very Magical Kingdom (v1.0)

Author: Author Harris
Title: Kingdom come
Series: Kingdom series
Series Index: 14.5
arberg is offline   Reply With Quote