![]() |
#1 |
Belgian Pommes Frites
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 122
Karma: 35038
Join Date: Jan 2012
Device: Pocketbook Touch HD
|
Question on a conditional regex
I have been banging my head against the wall trying to figure out how conditional regexes work.
I have looked at http://rexegg.com/regex-conditionals.html to figure it out, but no luck so far. My understanding of regexes has not advanced enough that I can put this level of regexes together. I want to have a conditional regex that says: if "font-size: 1.0em;" then leave it as it is. if font-size is anything else, then change it to font-size: xyz; And I want to do the same for the font-family: xyz; If possible, it should also work in text files, not just stylesheets, in case I find the occasional statement like: <span style="font-size: 1.0em">XXXXX</span> I would appreciate it if someone could help me with such a conditional regex. . ![]() |
![]() |
![]() |
![]() |
#2 |
A Hairy Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 3,347
Karma: 20171571
Join Date: Dec 2012
Location: Charleston, SC today
Device: iPhone 15/11/X/6/iPad 1,2,Air & Air Pro/Surface Pro/Kindle PW & Fire
|
My first impression is that conditionals are not supported under Sigil regex...but I have to admit, my regex-fu is not that strong... I'll be interest to see what the true experts have to say.
I would recommend not using styles within your html, you should use CSS files instead. <span style="font-size: 1.0em">XXXXX</span> is pure bloat. "Structure in the HTML and style in the CSS" Code:
CSS: p {font-size:1em} p.double {font-size:2em} HTML: <p>Normal paragraph.</p> <p>Normal paragraph.</p> <p>Normal paragraph.</p> <p class="double">Double font-size paragraph.</p> <p>Normal paragraph.</p> <p>Normal paragraph.</p> |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,568
Karma: 204127028
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Conditional subpatterns are supported in the PCRE version currently shipping with Sigil (PCRE 8.37).
https://www.pcre.org/original/doc/ht...ern.html#SEC21 |
![]() |
![]() |
![]() |
#4 |
Running with scissors
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,586
Karma: 14328510
Join Date: Nov 2019
Device: none
|
Another way to do it would be to delete all of the "font-size: 1.0em;" and put a "font-size: 100%;" on the body tag. Then all of the unadorned p tags would use 1em (using CSS's cascade). That's assuming that the p tag isn't enclosed in something where the font size is a different size, for example, an h2. I've had books where they apparently didn't understand the cascade and put a 1em font size on almost everything.
|
![]() |
![]() |
![]() |
#5 |
Belgian Pommes Frites
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 122
Karma: 35038
Join Date: Jan 2012
Device: Pocketbook Touch HD
|
It is in the first place to change stuff in the stylesheet.
I understand using styles that way is not very good, but sometimes I can not be bothered to remove it, and leave it as is with minimal changes, when I feel like being lazy. |
![]() |
![]() |
Advert | |
|
![]() |
#6 |
A Hairy Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 3,347
Karma: 20171571
Join Date: Dec 2012
Location: Charleston, SC today
Device: iPhone 15/11/X/6/iPad 1,2,Air & Air Pro/Surface Pro/Kindle PW & Fire
|
lol - I understand the need to be lazy sometimes. Although, with as much time as you put into figuring out conditional regex you could have replaced all those styles manually multiple times!
![]() |
![]() |
![]() |
![]() |
#7 |
Belgian Pommes Frites
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 122
Karma: 35038
Join Date: Jan 2012
Device: Pocketbook Touch HD
|
LoL..................
My Regex Fu does not allow me to leave it alone, when I get it into my mind to try out new regex stuff.....lol.................... The basic problem is however bigger: my brain seems to know only one setting when it tries to understand regexes: "ultra slow". And sometimes, the regexes crash my brain and the setting changes to "zero". The true cause of these crashes has not yet been determined, despite deep debugging research. Ah yes, the universe is still full of mysteries............................ Lol, |
![]() |
![]() |
![]() |
#8 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,568
Karma: 204127028
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
You're not alone. I consider my Fu to be fair-to-middlin', and conditionals just make my head hurt. I've yet to find a use for them, myself. Lookarounds usually suffice for my needs. For instance, the following expression should ignore any form of "font-size: 1em". Regardless of whether it's:
font-size: 1em font-size:1em font-size: 1.0em font-size:1.0em font-size: 1.00em font-size: 1em; Code:
font-size:(?!\s*(1(\.0+)?em))\s*\K[^(;|})]* I realize the regex conditional might have become your "white whale" at this point. If so, feel free to share any working solution you might eventually come up with. ![]() Last edited by DiapDealer; 12-18-2019 at 10:21 AM. |
![]() |
![]() |
![]() |
#9 |
Belgian Pommes Frites
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 122
Karma: 35038
Join Date: Jan 2012
Device: Pocketbook Touch HD
|
Oh yes, this is strong fu, this is nice fu. And I can use it in some other cases too. Perfect. Just what I needed.
Although lookarounds are still a bit of a mystery to me too, I at least understand what this regex does. Many thanks. |
![]() |
![]() |
![]() |
#10 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 28,568
Karma: 204127028
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Well that's 100% of 85% of the battle.
![]() EDIT: I actually had some extraneous parens in that expression. This one removes them (no change in the matching): Code:
font-size:(?!\s*1(\.0+)?em)\s*\K[^(;|})]* Last edited by DiapDealer; 12-18-2019 at 11:51 AM. |
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Regex question | rolgiati | Calibre | 2 | 11-26-2017 01:58 PM |
Regex question | theaccountant | Library Management | 0 | 11-07-2016 02:29 PM |
Regex question | Hendrixxxxxxxx | Sigil | 17 | 01-06-2016 09:24 PM |
Yet another regex question | Jabby | Sigil | 8 | 01-30-2012 08:41 PM |
Regex Question | Archon | Conversion | 11 | 02-05-2011 10:13 AM |