I’m using Calibre to convert a PDF to MOBI, but it has trouble interpreting space-indented code blocks. The blocks contain a lot of spaces, but in a lot of different amounts. Some lines are even indented by 31 spaces.
Calibre allows for 3 regexes to do search and replace in the book before it’s converted.
This is what I’ve tried.
\n( *) ( *)([a-zA-Z{};\*\/\(\)�-9])
Replace with:
\n\1 \2\3
The problem, it only replaces one of the spaces. I want them all replaced with the same abount of .
I’ve also tried lazy versions of the first group etc.
Is this one of the cases where regular expressions are insufficient? I think this regex engine is the python standard.
If this were Perl you could replace
(\G|\n)with$1 , and if it were a regex engine that allowed limited-width lookbehinds (instead of fixed-width lookbehinds like Python’s) you could replace(?<=\n {0,30})with ; but as it is, the only way I can think of is to replace something like((?<=\n)|(?<=\n )|(?<=\n {2})|(?<=\n {3})|(?<=\n {4})|(?<=\n {5})|...|(?<=\n {30}))with . . . and I suspect that at that point you’ll reach a limit on how long Calibre allows the input regex to be. :-/Another option is to take a completely different approach, and replace
(two spaces) with (non-breaking-space + regular space), without bothering to restrict it to the beginning of a line. I’m guessing that that will satisfy your needs?