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 03-21-2024, 10:28 AM   #1
lindlind
Junior Member
lindlind began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Mar 2024
Device: none
Question Regex in Regex function mode

Doing a search and replace like the one below in Regex mode works perfectly fine:

Search: </span>\s*<br/>\s*
Replace: xx

But when doing the same thing in Regex function, nothing happens:

Search: </span>\s*<br/>\s*
Function: return match.group().replace('</span>\s*<br/>\s*', 'xx')

It seems as if everything works fine if I do not use \s or quantifiers in the function. How can I do this right?
lindlind is offline   Reply With Quote
Old 03-21-2024, 10:35 AM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,866
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Function: return 'xx'
kovidgoyal is offline   Reply With Quote
Advert
Old 03-21-2024, 03:02 PM   #3
lindlind
Junior Member
lindlind began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Mar 2024
Device: none
I think I was not very clear, it tried to make it as simple as possible, showing that I can search for the string in Regex mode but not in Regex function mode. This just made it impossible to understand what I need...

The initial search is actually wider (<div.*?</div>), and I will do many replacements within the function, one of them is this:

string.replace('</span>\s*<br/>\s*', 'xx')

But it seems I cannot use \s or quantifiers in the function. Is there a way to do this?
lindlind is offline   Reply With Quote
Old 03-21-2024, 03:21 PM   #4
lindlind
Junior Member
lindlind began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Mar 2024
Device: none
additional note: I guess I should use something like re.sub instead, but is that possible?
lindlind is offline   Reply With Quote
Old 03-21-2024, 03:32 PM   #5
lomkiri
Zealot
lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.lomkiri ought to be getting tired of karma fortunes by now.
 
lomkiri's Avatar
 
Posts: 136
Karma: 1000102
Join Date: Jul 2021
Device: N/A
Quote:
Originally Posted by lindlind View Post
string.replace('</span>\s*<br/>\s*', 'xx')
But it seems I cannot use \s or quantifiers in the function. Is there a way to do this?
\s, \s* etc. are part of the regex language, the python str.replace() does not understand them.
You'll have to import re or regex and use sub or match :
Code:
import regex
string = regex.sub(r'</span>\s*<br/>\s*', 'xx', string)
(calibre uses mainly the module regex, but you can get the help from the python module "re", regex is a superset of re : https://docs.python.org/3/library/re.html. You can also use re, if you prefer)

--------------

Edit (I haven't seen your last message when I posted this one) :
Quote:
additional note: I guess I should use something like re.sub instead, but is that possible?
Yes, it is possible. You can import in the function any module that is part of the calibre package, and re and regex are in this case.
You may see my other message of today to get a practical exemple

Last edited by lomkiri; 03-22-2024 at 04:20 AM. Reason: spelling corrections
lomkiri is offline   Reply With Quote
Advert
Old 03-22-2024, 03:41 AM   #6
lindlind
Junior Member
lindlind began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Mar 2024
Device: none
Thanks a million!
lindlind is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with S&R RegEx Function MerlinMama Editor 5 08-16-2023 12:57 PM
Help creating possible Regex-Function MerlinMama Editor 14 03-03-2020 05:53 AM
Predefined regex for Regex-function sherman Editor 3 01-19-2020 05:32 AM
regex function replacement The_book Sigil 5 12-09-2019 09:45 AM
Regex Function about «» and “” senhal Editor 8 04-06-2016 02:12 AM


All times are GMT -4. The time now is 02:20 PM.


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