|
|
#1 |
|
Evangelist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 498
Karma: 32554
Join Date: May 2014
Location: Canada
Device: Kobo Sage
|
Regex - How to remove a word, capitalize next
Is there a way to replace "But they wanted..." with "They wanted..." in one search and replace operation, or do I need to continue with my current two-step process? (Remove 'But' in first, capitalize 'they' in the second)
|
|
|
|
|
|
#2 |
|
Bibliophagist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 48,776
Karma: 174510638
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Libra Colour, Lenovo M8 FHD, Paperwhite 4, Tolino epos
|
You could search for 'But ([a-z])' and replace with '\u\1' which will replace any But followed by a lower case letter with the upper case letter.
|
|
|
|
| Advert | |
|
|
|
|
#3 |
|
Evangelist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 498
Karma: 32554
Join Date: May 2014
Location: Canada
Device: Kobo Sage
|
I get the following error when I try to run it
Code:
calibre, version 6.26.0
ERROR: Unhandled exception: <b>error</b>:incomplete escape \u at position 3
calibre 6.26 embedded-python: True
Windows-10-10.0.22621-SP0 Windows ('64bit', 'WindowsPE')
('Windows', '10', '10.0.22621')
Python 3.10.1
Windows: ('10', '10.0.22621', 'SP0', 'Multiprocessor Free')
Interface language: None
Successfully initialized third party plugins: Count Pages (1, 13, 2) && Diaps Editing Toolbag (0, 4, 0) && FanFicFare (4, 27, 0) && Favourites Menu (1, 3, 0) && Find Duplicates (1, 10, 8) && Generate Cover (2, 3, 3) && Job Spy (1, 0, 239) && Kobo Utilities (2, 16, 10) && Save To Format (0, 0, 6) && View Manager (1, 10, 2)
Traceback (most recent call last):
File "calibre\gui2\tweak_book\boss.py", line 1129, in search
File "calibre\gui2\tweak_book\search.py", line 1562, in run_search
File "calibre\gui2\tweak_book\search.py", line 1469, in do_replace
File "calibre\gui2\tweak_book\editor\widget.py", line 328, in replace
File "calibre\gui2\tweak_book\editor\text.py", line 599, in replace
File "regex\regex.py", line 700, in _compile_replacement_helper
File "regex\_regex_core.py", line 1690, in _compile_replacement
File "regex\_regex_core.py", line 1757, in parse_repl_hex_escape
regex._regex_core.error: incomplete escape \u at position 3
|
|
|
|
|
|
#4 |
|
Bibliophagist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 48,776
Karma: 174510638
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Libra Colour, Lenovo M8 FHD, Paperwhite 4, Tolino epos
|
Sorry about that. I'm more used to the dialect of Regex used by Sigil which does accept \u to convert to one character to upper case.
According to a post by Kovid Goyal, calibre does not support \u and you will need to use function mode: https://manual.calibre-ebook.com/function_mode.html which is considerably more complex. Yet another reason that I use both calibre's ebook-editor and Sigil. They both offer capability that the other does not. Last edited by DNSB; 09-17-2023 at 03:17 PM. |
|
|
|
|
|
#5 |
|
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 173
Karma: 1497966
Join Date: Jul 2021
Device: N/A
|
Easy enough:
Search : But ([a-z]) regex-function : Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
return match[1].upper()
|
|
|
|
| Advert | |
|
|
|
|
#6 |
|
Evangelist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 498
Karma: 32554
Join Date: May 2014
Location: Canada
Device: Kobo Sage
|
Thank you, that worked! I really appreciate it.
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Capitalize first letter of each word | mortenanakin | Calibre | 1 | 08-06-2021 02:41 PM |
| Help! I broke my Capitalize Regex function. | staceytoob | Editor | 2 | 07-28-2021 10:48 PM |
| How to remove blank spaces in a word with RegEx? | RbnJrg | Sigil | 12 | 12-19-2018 07:58 AM |
| regex capitalize first letter | larysa | Editor | 8 | 10-23-2017 01:04 PM |
| [Old Thread] Capitalize first word in sentence with search and replace? | ianc | Conversion | 6 | 10-21-2013 05:47 PM |