Tudo sobre utilização de expressões regulares no Calibre

Regular expressions are features used in many places in calibre to perform sophisticated manipulation of e-book content and metadata. This tutorial is a gentle introduction to getting you started with using regular expressions in calibre.

First, a word of warning and a word of courage


Where in calibre can you use regular expressions?

There are a few places calibre uses regular expressions. There’s the Search & replace in conversion options, metadata detection from filenames in the import settings and Search & replace when editing the metadata of books in bulk. The calibre book editor can also use regular expressions in its Search and replace feature. Finally, you can use regular expressions when searching the calibre book list and when searching inside the calibre E-book viewer.

What on earth is a regular expression?



I’m using string here in the sense it is used in programming languages: a string of one or more characters, characters including actual characters, numbers, punctuation and so-called whitespace (linebreaks, tabulators etc.). Please note that generally, uppercase and lowercase characters are not considered the same, thus «a» being a different character from «A» and so forth. In calibre, regular expressions are case insensitive in the Search bar, but not in the conversion options. There’s a way to make every regular expression case insensitive, but we’ll discuss that later. It gets complicated because regular expressions allow for variations in the strings it matches, so one expression can match multiple strings, which is why people bother using them at all. More on that in a bit.

Care to explain?


That doesn’t sound too bad. What’s next?

Next is the beginning of the really good stuff. Remember where I said that regular expressions can match multiple strings? This is where it gets a little more complicated. Say, as a somewhat more practical exercise, the e-book you wanted to convert had a nasty footer counting the pages, like «Page 5 of 423». Obviously the page number would rise from 1 to 423, thus you’d have to match 423 different strings, right? Wrong, actually: regular expressions allow you to define sets of characters that are matched: To define a set, you put all the characters you want to be in the set into square brackets. So, for example, the set [abc] would match either the character «a», «b» or «c». Sets will always only match one of the characters in the set. They «understand» character ranges, that is, if you wanted to match all the lower case characters, you’d use the set [a-z] for lower- and uppercase characters you’d use [a-zA-Z] and so on. Got the idea? So, obviously, using the expression Page [0-9] of 423 you’d be able to match the first 9 pages, thus reducing the expressions needed to three: The second expression Page [0-9][0-9] of 423 would match all two-digit page numbers, and I’m sure you can guess what the third expression would look like. Yes, go ahead. Write it down.

Hey, neat! This is starting to make sense!

I was hoping you’d say that. But brace yourself, now it gets even better! We just saw that using sets, we could match one of several characters at once. But you can even repeat a character or set, reducing the number of expressions needed to handle the above page number example to one. Yes, ONE! Excited? You should be! It works like this: Some so-called special characters, «+», «?» and «*», repeat the single element preceding them. (Element means either a single character, a character set, an escape sequence or a group (we’ll learn about those last two later)- in short, any single entity in a regular expression). These characters are called wildcards or quantifiers. To be more precise, «?» matches 0 or 1 of the preceding element, «*» matches 0 or more of the preceding element and «+» matches 1 or more of the preceding element. A few examples: The expression a? would match either «» (which is the empty string, not strictly useful in this case) or «a», the expression a* would match «», «a», «aa» or any number of a’s in a row, and, finally, the expression a+ would match «a», «aa» or any number of a’s in a row (Note: it wouldn’t match the empty string!). Same deal for sets: The expression [0-9]+ would match every integer number there is! I know what you’re thinking, and you’re right: If you use that in the above case of matching page numbers, wouldn’t that be the single one expression to match all the page numbers? Yes, the expression Page [0-9]+ of 423 would match every page number in that book!





So, what are the most useful sets?



é equivalente a [0-9]


é equivalente a [a-zA-Z0-9_]


é equivalente a um espaço em branco


«Whitespace» is a term for anything that won’t be printed. These characters include space, tabulator, line feed, form feed, carriage return, non-breaking spaces, etc.



Fear not, life still is good and easy. Consider this example: The book you’re converting has «Title» written on every odd page and «Author» written on every even page. Looks great in print, right? But in e-books, it’s annoying. You can group whole expressions in normal parentheses, and the character "|" will let you match either the expression to its right or the one to its left. Combine those and you’re done. Too fast for you? Okay, first off, we group the expressions for odd and even pages, thus getting (Title)(Author) as our two needed expressions. Now we make things simpler by using the vertical bar ("|" is called the vertical bar character): If you use the expression (Title|Author) you’ll either get a match for «Title» (on the odd pages) or you’d match «Author» (on the even pages). Well, wasn’t that easy?

You can, of course, use the vertical bar without using grouping parentheses, as well. Remember when I said that quantifiers repeat the element preceding them? Well, the vertical bar works a little differently: The expression «Title|Author» will also match either the string «Title» or the string «Author», just as the above example using grouping. The vertical bar selects between the entire expression preceding and following it. So, if you wanted to match the strings «Calibre» and «calibre» and wanted to select only between the upper- and lowercase «c», you’d have to use the expression (c|C)alibre, where the grouping ensures that only the «c» will be selected. If you were to use c|Calibre, you’d get a match on the string «c» or on the string «Calibre», which isn’t what we wanted. In short: If in doubt, use grouping together with the vertical bar.

You missed…



Yes, I did, thanks for paying attention and reminding me. You can tell calibre how you want certain things handled by using something called flags. You include flags in your expression by using the special construct (?flags go here) where, obviously, you’d replace «flags go here» with the specific flags you want. For ignoring case, the flag is i, thus you include (?i) in your expression. Thus, (?i)test would match «Test», «tEst», «TEst» and any case variation you could think of.


I think I’m beginning to understand these regular expressions now… how do I use them in calibre?



New laws don't change that. </p>
<a href="http://www.processtext.com/abclit.html" class="calibre3">erter,
The court case gave us a revised version of what life was


Adicionar livros


Editar metadados em lote





Thanks for helping with tips, corrections and such:

  • ldolse

  • kovidgoyal

  • chaley

  • dwanthny

  • kacir

  • Starson17

  • Orpheu

For more about regexps see The Python User Manual. The actual regular expression library used by calibre is: regex which supports several useful enhancements over the Python standard library one.