![]() |
#1 |
Junior Member
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6
Karma: 2480
Join Date: May 2015
Device: none
|
Rechenfunktion mit RegEx
Hello friends,
I need your help: I edit with the Sigil editor an epub with about 3000 footnotes. I must implement these: Search: (<sup>)(\d+)(</sup>) Replace <a href="Anm.xhtml#\2+47"><sup id = "n\2+47">[\2+47]</sup></a> --- The footnote found is intended to be e.g. always add 47! --- Result: <a href="Anm.xhtml#253"><sup id="n253">[253]</sup></a> With RegEx, this is not possible, but I am on the net to the option: Function Mode for Search & Replace in the Editot of the new internal Caliber editor. Here, a RegEx term can be extended by a function using Python. Since I have never programmed with Python and would not want to learn it at 72 years, I ask you to help me solve my problem. MfG Andreas Last edited by calibre-ak-4711; 03-14-2017 at 03:05 PM. Reason: Google Translate |
![]() |
![]() |
![]() |
#2 |
The Fumbler
![]() Posts: 66
Karma: 10
Join Date: Jun 2015
Device: android 4.2/fbreader
|
Simple math for Regex
The following Regex-Function will add 47 to any number between <sup> and </sup>. You can then use normal Regex to make any other changes that you desire.
In the Find box put "<sup>(\d+)</sup>" without the quotes. In the Function box put "Add numbers" again without the quotes. The code required for "Add numbers" is as follows: Code:
import regex from calibre import replace_entities from calibre import prepare_string_for_xml def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): return ('<sup>' + str(47 + int(match.group(1))) + '</sup>') |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Junior Member
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6
Karma: 2480
Join Date: May 2015
Device: none
|
Simple math for Regex
Hello Thom,
thank you for your quick help! Your function works with INT: Return ('<sup>' + str (33 + int (match.group (1))) + '</ sup>') But not with LONG Return ('<sup>' + str (long(293) + long (match.group (1))) + '</ sup>') I hope you have a solution for this. PS: if you are over 70 years old, you should use your limited time for food, drink and pretty women! MfG Andreas Last edited by calibre-ak-4711; 03-15-2017 at 03:21 PM. |
![]() |
![]() |
![]() |
#4 |
The Fumbler
![]() Posts: 66
Karma: 10
Join Date: Jun 2015
Device: android 4.2/fbreader
|
I have no problem using either int or long data types to make this function work regardless of the size of the number. Is it possible that you are using an older version of Calibre'? I don't know what the problem is.
Regarding septuagenarians, my mistake, you are absolutely correct. |
![]() |
![]() |
![]() |
#5 |
Junior Member
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 6
Karma: 2480
Join Date: May 2015
Device: none
|
Simple math for Regex
Hello Thom,
SORRY: I searched: (<sup>)(\d+)(</sup>) instead of <sup>(\d+)</sup> !!! Thanks again MfG Andreas Last edited by calibre-ak-4711; 03-15-2017 at 05:37 PM. |
![]() |
![]() |
Advert | |
|
![]() |
#6 |
The Fumbler
![]() Posts: 66
Karma: 10
Join Date: Jun 2015
Device: android 4.2/fbreader
|
Also, the space in </ sup> might be problematic.
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Regex help please | BookJunkieLI | Calibre | 3 | 07-01-2014 03:18 PM |
Need help for a regex | wobohohoho | Sigil | 4 | 01-02-2013 04:42 AM |
Sync mit Handy etc. auch mit Nicht-Amazon-Büchern möglich! | Wurstbrot | Amazon Kindle | 14 | 03-28-2012 09:29 AM |
Regex | Gunnerp245 | Conversion | 5 | 03-05-2012 04:15 PM |
360 mit 15.3: Ansicht der ganzen Seite mit Zoom-Rahmen | mh445 | PocketBook | 0 | 01-03-2011 04:53 PM |