View Single Post
Old 03-24-2022, 10:02 PM   #1
enuddleyarbl
Guru
enuddleyarbl ought to be getting tired of karma fortunes by now.enuddleyarbl ought to be getting tired of karma fortunes by now.enuddleyarbl ought to be getting tired of karma fortunes by now.enuddleyarbl ought to be getting tired of karma fortunes by now.enuddleyarbl ought to be getting tired of karma fortunes by now.enuddleyarbl ought to be getting tired of karma fortunes by now.enuddleyarbl ought to be getting tired of karma fortunes by now.enuddleyarbl ought to be getting tired of karma fortunes by now.enuddleyarbl ought to be getting tired of karma fortunes by now.enuddleyarbl ought to be getting tired of karma fortunes by now.enuddleyarbl ought to be getting tired of karma fortunes by now.
 
enuddleyarbl's Avatar
 
Posts: 781
Karma: 1538394
Join Date: Sep 2013
Device: Kobo Sage
REGEX to Remove Embedded Header/Footer in the Text from a PDF?

Well, that title probably makes no sense. I've got the text from a PDF file that has the header and footer information embedded within it (so the header/footer looks just like actual text). Something like:

Quote:
blah blah blah

[page number] <== ex Footer

[title] <== ex Header

blah blah blah.
I'd like to convert that text to something passably readable as an EPUB via Calibre. But, before conversion, I need to get rid of those header/footer combinations. My REGEX knowledge is only microscopically above the zero point, and the best I could figure out as a way to find those headers/footers is:

Code:
\s+\d+\s+TITLE\s+
For my own future knowledge, I'll put what I think those codes mean in here:

\s means to match whitespace
+ means to match 1 or more
\d means to match a digit
TITLE is the title of the document that stuck in what used to be a header

So, it looks like that REGEX should grab from the start of the whitespace before the page number and run through the title to the end of the whitespace where the actual text picks up again. Probably not the best bit of REGEX, but it seems to work.

If the text before that header/footer combination is the end of a paragraph, that's fine. But, if the header/footer combination occurs right in the middle of a sentence, then removing it will result in the continuation "paragraph" being smashed right up against the paragraph that was before the header/footer.

For instance:

Quote:
Lit lognued in one of the gseut criahs in N’kcis ofcife, his lnog lges spilwarng
far asorcs the rgu. He was attauneted rehtar tahn bgi. Too mcuh of his chohdliod


11



TITLE

had been snept in fere flla. Now he cluod not fit itno a stadnard prussere siut
or sparcecaft cniba; and whvereer he sta, he lekood lkie he was tniyrg to tkae orev.
would be transmogrified to:

Quote:
Lit lognued in one of the gseut criahs in N’kcis ofcife, his lnog lges spilwarng
far asorcs the rgu. He was attauneted rehtar tahn bgi. Too mcuh of his chohdliodhad been snept in fere flla. Now he cluod not fit itno a stadnard prussere siut
or sparcecaft cniba; and whvereer he sta, he lekood lkie he was tniyrg to tkae orev.
Can anyone come up with a better way to strip out all those headers/footers?

EDIT: I guess if I replace the selection with a CR LF (/r/n), that would work reasonably. It doesn't look like it would be any worse than all the other lines ending with CR LF. I'll have to check and see if Calibre's conversion routine gets rid of those.

Last edited by enuddleyarbl; 03-24-2022 at 11:00 PM.
enuddleyarbl is offline   Reply With Quote