Avatar of tymccoy
tymccoy
 asked on

Regular expression substitution drops text before the match.

Trying to use regular expressions in C# to clean up span tags generated by GoogleDoc. The typical line has multiple span tags, and among them are spans that set the font weight to bold. I want to change only those bolding spans, but I am not able to write an expression that skips the content that precedes the bold span.


I know that the problem is that the match is triggered by the first span tag. What is the expression that skips all preceding span tags if they do not include a match of the target font string?


line1 = Regex.Replace(l, "<span style=.*font-weight: bold; .*;\" > ", " <span style='font-weight: bold'>");


line1 becomes this from that regex: 

<span style='font-weight: bold;'>we have created three “Top 10 Restaurant” lists.</span>



This is the original line line1:

<span style="font-size: 12pt; font-family: Calibri, sans-serif; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-variant-position: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;" data-mce-style="font-size: 12pt; font-family: Calibri, sans-serif; color: #000000; background-color: transparent; font-weight: 400; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-variant-position: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">With over 180 eateries on TCI, it can be tricky to decide where to dine. To make it easier for you to plan where to go, </span><span style="font-size: 12pt; font-family: Calibri, sans-serif; color: #000000; background-color: transparent; font-weight: bold; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-variant-position: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;" data-mce-style="font-size: 12pt; font-family: Calibri, sans-serif; color: #000000; background-color: transparent; font-weight: bold; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-east-asian: normal; font-variant-position: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">we have created three “Top 10 Restaurant” lists.</span>

Regular ExpressionsC#

Avatar of undefined
Last Comment
gr8gonzo

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
gr8gonzo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck