Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 正则表达式以匹配单个CSS属性_.net_Css_Regex - Fatal编程技术网

.net 正则表达式以匹配单个CSS属性

.net 正则表达式以匹配单个CSS属性,.net,css,regex,.net,Css,Regex,我目前有一大批HTML文本,我有几个CSS属性类似于以下内容: font:16px/normal Consolas; font:16px/normal Arial; font:12px/normal Courier; 它还与其他几个CSS属性和其他关联的HTML值和标记捆绑在一起 我一直在尝试编写一个正则表达式,它只会获取这些“字体样式”,因此如果我有以下两段话: <p style='font:16px/normal Arial; font-weight: x; color: y;'&g

我目前有一大批HTML文本,我有几个CSS属性类似于以下内容:

font:16px/normal Consolas;
font:16px/normal Arial;
font:12px/normal Courier;
它还与其他几个CSS属性和其他关联的HTML值和标记捆绑在一起

我一直在尝试编写一个正则表达式,它只会获取这些“字体样式”,因此如果我有以下两段话:

<p style='font:16px/normal Arial; font-weight: x; color: y;'>Stack</p>
<span style='color: z; font:16px/normal Courier;'>Overflow</span>
<br />
<div style='font-family: Segoe UI; font-size: xx-large;'>Really large</div>
结果如下:

font:bold;                   //Match
font:12pt/normal Arial;      //Match
font:16px/normal Consolas;   //Match
font:12pt/normal Arial;      //Match
property: value;             //Not a Match
property: value value value; //Not a Match
但当我试图插入一大块HTML时,事情似乎变得混乱起来,大块被选中,而不是在先前指定的范围内


我很乐意提供我所能提供的任何其他信息和测试数据。

您留下了贪婪的
*
,这意味着它将不停地吃,并且只在可用的最后一个分号处停止。添加问号,即
*?
,使其不贪婪

更新:

    \b(?:font\s*?:\s*([^;>]*?)(?=[;">}]))

我已经在测试了这个页面上的每个示例。

我不太确定您在问什么,但我认为这个问题可以通过用CSS替换样式标记来解决。这个问题可以通过在HTML的Head标记中放置以下内容来解决

<style type="text/css">

h1 {

    font-family: Arial;
    font-size: 15;
    font-style:oblique;

}

h2 {
    font-family: Courier;
    font-size: 16;
    font-style:oblique;
 }
 h3 {
    font-family: Segoe UI;
    font-size: xx-large;
    font-style:oblique;
 }


</style>

h1{
字体系列:Arial;
字号:15 ;;
字体风格:斜体;
}
氢{
字体系列:信使;
字号:16 ;;
字体风格:斜体;
}
h3{
字体系列:SegoeUI;
字体大小:xx大号;
字体风格:斜体;
}
现在,要使一个表达式(或您自己)设置这些字体样式之一,您所要做的就是用如下标记将其包围:

<h1> Cool Text!  </h1>
酷文本!
祝你好运

试试这个正则表达式:

(?:font:[^;]*);

它匹配
font:16px/normal Arial
font:16px/普通快递试试这个

\b((?:font:[^;]*?)(?:;|'))
解释

\b             # Assert position at a word boundary
(              # Match the regular expression below and capture its match into backreference number 1
   (?:            # Match the regular expression below
      font:          # Match the characters “font:” literally
      [^;]           # Match any character that is NOT a “;”
         *?             # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
   )
   (?:            # Match the regular expression below
                     # 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)
         &apos;              # Match the character “&apos;” literally
   )
)
我建议:

\bfont\s*:\s*([^;}"'<>]+)(?<=\S)

您不应该使用分隔符来获得精确匹配吗?类似于^(\b(?:font[\s*\]*:[\s*\]*?(\b.*\b);)$很遗憾,在这种情况下,我无法控制输入。您可以通过编写PHP或Javascript代码来“控制”输入,只需在前面添加这些代码。很简单,谢谢。我对正则表达式的了解似乎永远不够。请注意
font:sans serif 80%将永远不会匹配。即使在更新之后,我仍然看到两种可能失败的方法,尽管这种情况应该非常罕见。首先,显然,注释
/**/在CSS中;第二,字体样式值中出现的一个
[;“>}]
字符是可能的,除了双引号,但只有在属性使用单引号的情况下才可能出现,谢谢Barry,到目前为止,这似乎是可行的(通常我觉得正则表达式过于复杂)如果字体样式在其组中是最后一个,并且没有用分号结束,则此操作将失败。谢谢Cylian-此操作同样有效,并且解释非常有用。
\bfont\s*:\s*([^;}"'<>]+)(?<=\S)
.foo { font: sans-serif 80% }
... style="font: sans-serif 80%" ...