regexreplaceepubcalibre

How to perform regex substitutions on chapter including theirs numbers with Calibre?


I have the following sequence in a epub file:

CHAPTER 1
CHAPTER 2
CHAPTER 3

I want to add a head tag for each of them, so i use the replacement module of Calibre:

Find: CHAPTER *
Replace with:<h1>CHAPTER</h1>

But i only obtain this:

<h1>CHAPTER</h1>1
<h1>CHAPTER</h1>2
<h1>CHAPTER</h1>3

... with the chapter number outside the h1 tag. What am I doing wrong?


Solution

  • Regular expressions don't work like wildcard characters.

    Indeed, the regex CHAPTER * means : "CHAPTER followed by none or several spaces".

    You need to catch the string "CHAPTER" followed by at least one blank character then by a number (integer).

    So, you should write :

    Find: CHAPTER (\d+)
    Replace with:<h1>CHAPTER \1</h1>
    

    Here, (\d+) is a sequence of at least one (+) digit (\d). The enclosing parentheses capture this sequence and store it as \1

    You should read the Quick reference for regexp syntax of Calibre for more details.