03-21-2024, 11: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, 11:35 AM | #2 |
creator of calibre
Posts: 44,535
Karma: 24495948
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Function: return 'xx'
|
03-21-2024, 04: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, 04: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, 04:32 PM | #5 | ||
Zealot
Posts: 148
Karma: 1451628
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 05:20 AM. Reason: spelling corrections |
||
03-22-2024, 04:41 AM | #6 |
Junior Member
Posts: 6
Karma: 10
Join Date: Mar 2024
Device: none
|
Thanks a million!
|
Thread Tools | Search this Thread |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Help with S&R RegEx Function | MerlinMama | Editor | 5 | 08-16-2023 01:57 PM |
Help creating possible Regex-Function | MerlinMama | Editor | 14 | 03-03-2020 06:53 AM |
Predefined regex for Regex-function | sherman | Editor | 3 | 01-19-2020 06:32 AM |
regex function replacement | The_book | Sigil | 5 | 12-09-2019 10:45 AM |
Regex Function about «» and “” | senhal | Editor | 8 | 04-06-2016 03:12 AM |