MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Sigil (https://www.mobileread.com/forums/forumdisplay.php?f=203)
-   -   Question on a conditional regex (https://www.mobileread.com/forums/showthread.php?t=325706)

Capricorn 12-16-2019 07:17 AM

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.
.:thanks:

Turtle91 12-16-2019 08:51 AM

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>


DiapDealer 12-16-2019 10:30 AM

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

hobnail 12-16-2019 02:17 PM

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.

Capricorn 12-16-2019 03:06 PM

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.

Turtle91 12-17-2019 03:54 PM

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! :D

Capricorn 12-18-2019 02:06 AM

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,

DiapDealer 12-18-2019 11:14 AM

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[^(;|})]*
You should then be able to replace the resulting match with any value you like.

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. ;)

Capricorn 12-18-2019 12:16 PM

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.

DiapDealer 12-18-2019 12:39 PM

Quote:

Originally Posted by Capricorn (Post 3930146)
I at least understand what this regex does.

Well that's 100% of 85% of the battle. :D

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[^(;|})]*


All times are GMT -4. The time now is 10:29 PM.

Powered by: vBulletin
Copyright ©2000 - 3.8.5, Jelsoft Enterprises Ltd.
MobileRead.com is a privately owned, operated and funded community.