View Single Post
Old 02-14-2011, 09:35 AM   #3
Archon
Zealot
Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!Archon , Klaatu Barada Niktu!
 
Archon's Avatar
 
Posts: 110
Karma: 5176
Join Date: Dec 2010
Device: Mac OSX, iPad, iPod, & Nook
Wow! Sorry Manichean but the Python documentation you pointed to is for experienced programmers. I can't hardly understand anything on that page and I read it several times.

It is definitely not a beginners guide to regular expressions and I think what cybmole was looking for was a simple explanation of how the regular expression that he posted works.

Something like:
(?P<author>.+) - (?P<title>[^_]+)

The first part of the expression up through the hyphen takes a book's filename (any character contained) and puts the first part of the filename up to the space before the hyphen as the "author" when it adds the book.
The second part does the same thing for the "title". The "[^_]" in the second part tells it to ignore underscores in titles.

I am not entirely sure of my explanation but the Python manual makes too many references to things you don't need to know at the beginning level (octal numbers and nulls?) of understanding regular expressions. And as far as I have learned so far the "?P" is specific to Python's "flavor" of regex and is not found in other regex's so that is why you could not find it cybmole.

I think it appears as though Calibre's Python uses (?P<foo>) to pass the contained value of the parentheses to the variable "foo" as other regexes use \1 to pass the contained value of the first set of parentheses to the replace field to be used in a replacement expression.

Check out chapter 8 of the TextWrangler manual. Even if you don't use TextWrangler it is a good beginners guide to regex.
You can download it here:
http://www.barebones.com/support/textwrangler/
About 3/4 of the way down the page.

Happy Monday
Archon

Last edited by Archon; 02-14-2011 at 09:47 AM.
Archon is offline   Reply With Quote