Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Editor

Notices

Reply
 
Thread Tools Search this Thread
Old Today, 09:59 AM   #1
lindlind
Junior Member
lindlind began at the beginning.
 
Posts: 5
Karma: 10
Join Date: Mar 2024
Device: none
Replace group in Regex function mode

I have the following string:

Code:
<span class="line">Come here, he says.<span aria-label="11" class="page-normal" id="d5e1496" role="doc-pagebreak" title="11" epub:type="pagebreak"></span></span><br />
If I do a search and replace in Regex mode like this:

Search:
Code:
<span([^>]*"pagebreak"> ?)</span>
Replace:
Code:
<xxspan\1</xxspan>
The result is:

Code:
<span class="line">Come here, he says.<xxspan aria-label="11" class="page-normal" id="d5e1496" role="doc-pagebreak" title="11" epub:type="pagebreak"></xxspan></span><br />

However, if I add the following in a Regex-function:

Code:
return regex.sub('<span([^>]*"pagebreak"> ?)</span>', '<xxspan\1</xxspan>', match.group())
The result is this:

Code:
<span class="line">Come here, he says.<xxspan</xxspan></span><br />
What am I doing wrong here?
lindlind is offline   Reply With Quote
Old Today, 12:50 PM   #2
thiago.eec
Guru
thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.
 
Posts: 963
Karma: 1183425
Join Date: Dec 2016
Location: Goiânia - Brazil
Device: iPad, Kindle Paperwhite
Python does not understand '\1' as a capturing group. You need to use 'match.groups()[0]' to get the first capturing group.

So, just use it like this:
Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    return '<xxspan' + match.groups()[0] + '</xxspan>'
thiago.eec is offline   Reply With Quote
Advert
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to replace text with Search and Replace with regex on Calibre darrnih ePub 2 04-02-2024 02:10 AM
Regex in Regex function mode lindlind Editor 5 03-22-2024 03:41 AM
[Plugin] regex function replace plugin The_book Plugins 19 09-23-2022 07:58 AM
Can we run Function mode in Search & replace during the conversion process? omin Conversion 2 02-08-2020 08:53 PM
Missing "function mode" for editor's Search & Replace atux Editor 3 01-17-2016 08:40 AM


All times are GMT -4. The time now is 07:57 PM.


MobileRead.com is a privately owned, operated and funded community.