====== Useful regular expressions ====== A relatively advanced way to potentially semi-automate a few processes is the use of regular expressions. These are basically an advanced form of “find and replace” which can be very powerful. When used properly, they can clean up certain code and properly format things like footnotes in record time, or convert square bracket''[1]'' style footnotes to without''1'' for print, and vice versa ((Concretely, you would look for something like ''class="noteref">(.*?)'' and replace this with something like ''>[\1]'')). For example, from the final Wellred InDesign book to [[publishing:digital:ebook|EPUB export]], we can now often achieve a 100% finished ebook in less than an hour thanks to a series of regular expressions executed at once. The easiest way to understand the concept is to use a simple one. A fairly typical export from InDesign will have Headers as a paragraph tag, which isn't much use for HTML/EPUB: Rather than ''
Chapter title
'' we would like ''(.*?)
'' ''(.*?)'' is a variable that captures everything in between the paragraph tag with class ''"Headings_Introduction-Title"''. To convert this to a proper]+>
with
=====remove span tags:=====
Replace
<[/]?span[^>]*>
with nothing
=====Remove empty paragraph tags:=====
Replace
]*>
with nothing
Replace
]*>[ ]?
with nothing
=====Bold paragraphs to h4:=====
Replace
]*>([^<]+)
]*>([^<]+)
With:
\1
=====Handling footnotes:=====
(\[[0-9]\])
(\[[0-9]\])
\3
\1
\1
([a-z]*)
\3
([\.”!])[ ]*([0-9]{1,2})([ <])
\1\2\3
=====Finding and replacing double quotation:=====
(?]*)("|'')(?!>)(\W)
“\1”\3
"([^"\n]+)
“\1
(?]*)(')(?!>)(\W)
‘\1’\3
=====Removing line breaks in code:=====
This is useful if above isn't working because of line breaks.
Replace
\n
with
Replace
\r\n
with