5

現在、大量のHTMLテキストがあり、次のようなCSSプロパティがいくつかあります。

font:16px/normal Consolas;
font:16px/normal Arial;
font:12px/normal Courier;

これは、他のいくつかのCSSプロパティおよび他の関連するHTML値とタグにもバンドルされています。

私はこれらの「フォントスタイル」のみを取得する正規表現を作成しようとしてきたので、次の2つの段落がある場合:

<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:セミコロンで始まり、セミコロンで終わるプロパティにのみ一致します;

私はRegexHeroを使って遊んだことがあり、最も近いものは次のとおりです。

\b(?:font[\s*\\]*:[\s*\\]*?(\b.*\b);)

これにより、次の結果が得られました。

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の大きなブロックをドロップしようとすると、物事が混乱しているように見え、以前に指定した範囲内ではなく、大きなブロックが選択されました。

可能な限りの追加情報とテストデータを提供させていただきます。

4

5 に答える 5

5

これを試して

\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 “&amp;apos;” literally
   )
)
于 2012-06-13T15:13:03.463 に答える
4

あなたは.*貪欲なままになりました。つまり、それは食べて食べて、利用可能な最後のセミコロンでのみ停止します。疑問符を追加します。つまり.*?、欲張りでないようにします。

更新しました:

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

このページhttp://rubular.com/r/yRcED2n6wu )のすべての例をテストしました。

于 2012-06-13T15:10:13.433 に答える
2

この正規表現を試してください:

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

上記のスニペットと一致font:16px/normal Arial;します。font:16px/normal Courier;

于 2012-06-13T15:11:02.660 に答える
1

私は提案します:

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

これは、他の回答が失敗した場合にも機能します。例えば:

.foo { font: sans-serif 80% }
... style="font: sans-serif 80%" ...
于 2012-06-13T16:02:58.750 に答える
0

何を求めているのかよくわかりませんが、スタイルタグを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>

これで、式(または自分自身)にこれらのフォントスタイルの1つを設定するために必要なのは、次のようなタグで囲むことだけです。

<h1> Cool Text!  </h1>

幸運を!

于 2012-06-13T15:11:00.117 に答える