2

我尝试使用正则表达式<font style="font-size:85%;font-family:arial,sans-serif">

font-size:85%;

我的正则表达式是^font-size:(*);

我的意思是我必须完全删除字体大小标签。

有人可以帮我吗?

谢谢!

4

2 回答 2

4

您当前的正则表达式有几件事会导致它失败:

^font-size:(*);

您正在锚定到行首^- 属性不在行首。

*就其本身而言,没有任何意义。

将其更改为:

font-size: ?\d{1,2}%;
于 2012-09-12T14:06:09.960 回答
3

这是您需要的正则表达式:

string html = @"<font style=""font-size:85%;font-family:arial,sans-serif"">";
string pattern = @"font-size\s*?:.*?(;|(?=""|'|;))";
string cleanedHtml = Regex.Replace(html, pattern, string.Empty);

即使在orfont-size中定义了,或者定义了一组不同的 CSS 样式(即未指定),此正则表达式也将起作用。你可以在这里看到结果。ptemfont-family

正则表达式的解释如下:

// font-size\s*?:.*?(;|(?="|'|;))
// 
// Match the characters “font-size” literally «font-size»
// Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*?»
//    Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the character “:” literally «:»
// Match any single character that is not a line break character «.*?»
//    Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the regular expression below and capture its match into backreference number 1 «(;|(?="|'|;))»
//    Match either the regular expression below (attempting the next alternative only if this one fails) «;»
//       Match the character “;” literally «;»
//    Or match regular expression number 2 below (the entire group fails if this one fails to match) «(?="|'|;)»
//       Assert that the regex below can be matched, starting at this position (positive lookahead) «(?="|'|;)»
//          Match either the regular expression below (attempting the next alternative only if this one fails) «"»
//             Match the character “"” literally «"»
//          Or match regular expression number 2 below (attempting the next alternative only if this one fails) «'»
//             Match the character “'” literally «'»
//          Or match regular expression number 3 below (the entire group fails if this one fails to match) «;»
//             Match the character “;” literally «;»
于 2012-09-12T14:06:01.020 回答