![]() |
#1 |
Junior Member
![]() Posts: 6
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 /> Search: Code:
<span([^>]*"pagebreak"> ?)</span> Code:
<xxspan\1</xxspan> 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()) Code:
<span class="line">Come here, he says.<xxspan</xxspan></span><br /> |
![]() |
![]() |
![]() |
#2 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,169
Karma: 1270433
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>' |
![]() |
![]() |
![]() |
#3 |
Junior Member
![]() Posts: 6
Karma: 10
Join Date: Mar 2024
Device: none
|
Thank you, I couldn't make that work either, but tried this, which works!
Code:
p = regex.compile('<span([^>]*"pagebreak"> ?)</span>') return p.sub(r'<xxspan\1</xxspan>', match.group()) |
![]() |
![]() |
![]() |
|
![]() |
||||
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 03:10 AM |
Regex in Regex function mode | lindlind | Editor | 5 | 03-22-2024 04:41 AM |
[Plugin] regex function replace plugin | The_book | Plugins | 19 | 09-23-2022 08:58 AM |
Can we run Function mode in Search & replace during the conversion process? | omin | Conversion | 2 | 02-08-2020 09:53 PM |
Missing "function mode" for editor's Search & Replace | atux | Editor | 3 | 01-17-2016 09:40 AM |