Thread: Regex examples
View Single Post
Old 12-15-2016, 02:49 AM   #511
Nando Sandiego
Junior Member
Nando Sandiego began at the beginning.
Posts: 9
Karma: 10
Join Date: Oct 2016
Device: Hypen app for iOS
css option

Originally Posted by Psymon View Post
Hey, folks -- I am trying to learn/do this regex stuff on my own (however slowly)! I'm stumped on something that I would think should be fairly easy, though.

In my book, I've got almost 300 paragraphs that start off with a dropcap, with this being an example of how those paragraphs begin...

<span class="initial">H</span>onourable
What I want to do is make that first word in smallcaps, and so the code in this latter example would then be...

<span class="initial">H</span><span class="smallcaps">ONOURABLE</span>
So basically what I want to do is convert the case of that first word to uppercase and then wrap that smallcaps span around the relevant part of the word.

For my regex search I initially came up with this...

<span class=\"initial\">(.+?)</span>([^>]*)\s

...and for replace this...

<span class="initial">\1</span><span class="smallcaps">\U\2\E</span>

...(and in this latter there's an invisible space there that I suppose you won't "see" in this post -- but it would be there in my S&R, of course).

For the life of me, though, that \s won't stop at the first space, that is, after the first word -- it selects the entire paragraph up to the last space in the paragraph! -- and it's also possible that there might actually be not a space, but a comma (or other punctuation) instead, and I'd like that closing span (for my smallcaps) to come before that.

I've searched around the 'net trying to find the solution to this, but just can't seem to find it -- every "answer" that I find on other sites and try just doesn't seem to work.

Thanks in advance, if anyone can help!

(PS. I'm not sure if my "replace" code is correct either, actually -- although I never got that far with figuring this out!)
I was formatting documents similar to the way you your were but used:
<span class=\"initial\">(.*?)</span>(.*?)\s
and did chapters one at a time.

I since discovered this css trick that lets me assign a paragraph class that make the first letter a drop cap while automatically accounting for quotes and also starting with span (<p class="first><span class=<italic">), or <i>, <em>, and seemingly anything until the first letter is found.

The only condition I have found where the first letter is not drop capped is if dash, en-dash or em-dash are the first character.

I also set the entire first line of text as smallcaps dynamically, meaning smallcaps for the first line no matter how many words are displayed. The result is much like books are formatted.

As a result I do much less regex editing than I was doing before.

The css is:

.parafirst { your format options for first paragraph }
.parafirst:first-letter { your format options for drop cap }
.parafirst:first-line { font-variant: small-caps; } < could also be any format you like, such as bold or convert to all uppercase

I found the above solution looking for a way for css to handle a first word of a paragraph but that does not seem to exist. Just letter and line as options.
Nando Sandiego is offline   Reply With Quote