I have a piece of html code that I want to wipe out some style parts, I know I need to regex but I don't know how to generate the regex or even how to apply it in my c# code. Below is the sample of original string:
<p style="color: #000000; text-transform: none; letter-spacing: normal; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px;">
And here is the output that I wish to get after a replace operation:
<p>
I want to get rid of the style attribute. And I need to do this for all occurances of <p ...>
There exist tons of examples about this kind of jobs, but I really got confused about this. So any clue on solution would be great. Thanks in advance.
You really find a regex tutorial ( example ) to learn how matches work, then replacements will be easier...
string output = Regex.Replace(input, @"(?<=<p)[^>]+", "");
See demo .
To remove only the style attribute, you could perhaps use this:
string output = Regex.Replace(input, @"(?<=<p)\s*style=""[^""]+""", "");
Note that this won't work if the style attribute is immediately after the <p
(with any number of spaces).
To remove the attribute style anywhere in the html, you can perhaps use (a bit safer than the previous one maybe):
string output = Regex.Replace(input, @"(?<=<p)([^>]*?)\s*style=""[^"">]+""", "$1");
Not sure how to do it in c#, but using a general example in bash regex, I would do:
echo "$pattern" | sed -r 's/(<p).*(>)/\1\2/'
Where:
(<p) ----- Captures the opening bracket with p
.* ----- Anything inbetween up to the next ">"
() ----- Captures the closing bracket
\1\2 ----- Gives you back the two captured things,
in this order, with no space inbetween
Hope it helps, but again, you need to look up for replacing in c# yourself.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.