03-21-2024, 10:28 AM | #1 |
Junior Member
Posts: 6
Karma: 10
Join Date: Mar 2024
Device: none
|
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? |
03-21-2024, 10:35 AM | #2 |
creator of calibre
Posts: 44,380
Karma: 23766374
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Function: return 'xx'
|
03-21-2024, 03:02 PM | #3 |
Junior Member
Posts: 6
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? |
03-21-2024, 03:21 PM | #4 |
Junior Member
Posts: 6
Karma: 10
Join Date: Mar 2024
Device: none
|
additional note: I guess I should use something like re.sub instead, but is that possible?
|
03-21-2024, 03:32 PM | #5 | ||
Zealot
Posts: 139
Karma: 1000102
Join Date: Jul 2021
Device: N/A
|
Quote:
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) -------------- Edit (I haven't seen your last message when I posted this one) : Quote:
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 |
||
03-22-2024, 03:41 AM | #6 |
Junior Member
Posts: 6
Karma: 10
Join Date: Mar 2024
Device: none
|
Thanks a million!
|
|
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 |