|
|
#1 |
|
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 348
Karma: 2581190
Join Date: Dec 2013
Location: LaVernia, Texas
Device: kindle epub readers on android
|
regex class search
Sometimes the simplest things really give me a kick. I use classes almost exclusively instead of styles. My CSS is a long list of classes in the main consisting of the first letter of each word in every which kind of style definition. In this way I can quickly hop and chop from this to that. I got stumped on how to use calibre's 'find' for any kind of class name consisting of one or more letters. It got lost due to being surrounded by spaces, other classes, or quotation marks. So I asked AI and it gave me: class="[^"]*X[^"]*"
I remove X and put in whatever I am looking for, then make sure the search says 'regex' instead of 'fuzzy.' I also put it in 'saved searches' in case I forget. |
|
|
|
|
|
#2 | |
|
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,091
Karma: 1084520
Join Date: Sep 2017
Location: Buenos Aires, Argentina
Device: moon+ reader, kindle paperwhite
|
Quote:
Exact Search (Without False Positives) If you want to find the exact class `bc` (for example, `background-color`) and avoid finding `abc` or `bcc`, use this variation: `class="[^"]*(^|\s)bc(\s|$)[^"]*"` `(^|\s)`: Ensures that your class name is preceded by a quote or a space. `(\s|$)`: Ensures that it is followed by a space or a closing quote. Searching for Classes Starting with...` If, on the other hand, you want to see all the classes that define something related to the border (assuming they all start with `b`), you can use: `class="[^"]*(^|\s)b[a-z]*(\s|$)[^"]*"` |
|
|
|
|
|
|
#3 |
|
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,808
Karma: 9501034
Join Date: Sep 2021
Location: Australia
Device: Kobo Libra 2
|
Curious to know how many errors are reported when using epubCheck and checkbook.
|
|
|
|
|
|
#4 | ||
|
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 190
Karma: 1537710
Join Date: Jul 2021
Device: N/A
|
Quote:
(^|\s)bc : Ensures that bc is at the beginning of the line (which never happens since you put class="[^"]* before this expression) or after \s bc(\s|$) : same problem, ensures that bc is at the EoL (never happens) or before \s So it would select class="ab bc de" but neither class="ab bc" nor class="bc cd ef" A working regexp would be : class="[^"]*?\s*\Kbc(?=[\s"]) it will select only bc in the class (only the first one if there is several of it) \K asks for resetting the chain (forget what is before \K) (?=[\s"]) is a lookahead, it selects bc only if it's followed by \s or " Quote:
In that case, this one does the job (finds only the 1st one): class="[^"]*?\s*\Kbc[\w-]*(?=[\s"]) or, if you want bc and up to 3 more char (e.g. bc, bcd, bcdef, bc-d, bc_de) class="[^"]*?\s*\Kbc[\w-]{0,3}(?=[\s"]) (change {0,3} for whatever number you need) Last edited by lomkiri; 03-28-2026 at 09:11 PM. |
||
|
|
|
|
|
#5 |
|
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,091
Karma: 1084520
Join Date: Sep 2017
Location: Buenos Aires, Argentina
Device: moon+ reader, kindle paperwhite
|
You're right, Lomkiri. My approach with (^|\s) and (\s|$) aimed to emulate word boundaries, but indeed, start and end line anchors can cause problems depending on the HTML formatting. That's why I suggested trying it, but your use of \K is much more precise, especially since it allows you to select only the class without dragging the class=" attribute. Great contribution!
|
|
|
|
|
|
#6 |
|
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 190
Karma: 1537710
Join Date: Jul 2021
Device: N/A
|
You're right, with word boundaries (\b), my 2nd and 3rd regex (for words starting with bc) are more elegant:
class="[^"]*?\K\bbc[\w-]*\b class="[^"]*?\K\bbc[\w-]{0,3}\b In the 1st one, we have to keep the lookahead to not have a match with "bc" in "bc-de" class="[^"]*?\K\bbc(?=[\s"]) Last edited by lomkiri; 03-29-2026 at 05:55 AM. |
|
|
|
|
|
#7 | |
|
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 348
Karma: 2581190
Join Date: Dec 2013
Location: LaVernia, Texas
Device: kindle epub readers on android
|
Quote:
|
|
|
|
|
|
|
#8 | |
|
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,808
Karma: 9501034
Join Date: Sep 2021
Location: Australia
Device: Kobo Libra 2
|
Quote:
A couple of things don't look quite right in your screenshot. |
|
|
|
|
![]() |
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Regex replace p-class in entire book | AlwaysNew | Editor | 17 | 07-10-2020 09:11 AM |
| Regex for replacing class | AlwaysNew | Editor | 5 | 06-22-2020 08:55 AM |
| Regex in search problems (NOT Search&Replace; the search bar) | lairdb | Calibre | 3 | 03-15-2017 07:10 PM |
| Regex Search doesn't search all files in Edit Book | GregTheGrate | Editor | 8 | 11-08-2016 12:47 AM |
| Help with regex POSIX class search | bfollowell | Sigil | 7 | 05-21-2011 10:55 AM |