Regexを使用してHTMLから値を抽出する方法は?
-
26-10-2019 - |
質問
次のHTMLが与えられます:
<p><span class="xn-location">OAK RIDGE, N.J.</span>, <span class="xn-chron">March 16, 2011</span> /PRNewswire/ -- Lakeland Bancorp, Inc. (Nasdaq: <a href='http://studio-5.financialcontent.com/prnews?Page=Quote&Ticker=LBAI' target='_blank' title='LBAI'> LBAI</a>), the holding company for Lakeland Bank, today announced that it redeemed <span class="xn-money">$20 million</span> of the Company's outstanding <span class="xn-money">$39 million</span> in Fixed Rate Cumulative Perpetual Preferred Stock, Series A that was issued to the U.S. Department of the Treasury under the Capital Purchase Program on <span class="xn-chron">February 6, 2009</span>, thereby reducing Treasury's investment in the Preferred Stock to <span class="xn-money">$19 million</span>. The Company paid approximately <span class="xn-money">$20.1 million</span> to the Treasury to repurchase the Preferred Stock, which included payment for accrued and unpaid dividends for the shares.  This second repayment, or redemption, of Preferred Stock will result in annualized savings of <span class="xn-money">$1.2 million</span> due to the elimination of the associated preferred dividends and related discount accretion.  A one-time, non-cash charge of <span class="xn-money">$745 thousand</span> will be incurred in the first quarter of 2011 due to the acceleration of the Preferred Stock discount accretion.  The warrant previously issued to the Treasury to purchase 997,049 shares of common stock at an exercise price of <span class="xn-money">$8.88</span>, adjusted for stock dividends and subject to further anti-dilution adjustments, will remain outstanding.</p>
内部に値を取得したいのですが <span>
要素。また、の価値を取得したいと思います class
の属性 <span>
要素。
理想的には、関数を介してHTMLを実行し、抽出されたエンティティの辞書を取り戻すことができました( <span>
上記で定義された解析)。
上記のコードは、より大きなソースHTMLファイルからのスニペットです。これはXMLパーサーでパレーできません。だから、私は興味のある情報を抽出するのに役立つ可能性のある正規表現を探しています。
解決
このツールを使用(無料):http://www.radsoftware.com.au/regexdesigner/
この正規表現を使用してください:
"<span[^>]*>(.*?)</span>"
グループ1の値(各一致)は、必要なテキストになります。
C#では、次のようになります。
Regex regex = new Regex("<span[^>]*>(.*?)</span>");
string toMatch = "<span class=\"ajjsjs\">Some text</span>";
if (regex.IsMatch(toMatch))
{
MatchCollection collection = regex.Matches(toMatch);
foreach (Match m in collection)
{
string val = m.Groups[1].Value;
//Do something with the value
}
}
コメントに答えるために補償されました:
Regex regex = new Regex("<span class=\"(.*?)\">(.*?)</span>");
string toMatch = "<span class=\"ajjsjs\">Some text</span>";
if (regex.IsMatch(toMatch))
{
MatchCollection collection = regex.Matches(toMatch);
foreach (Match m in collection)
{
string class = m.Groups[1].Value;
string val = m.Groups[2].Value;
//Do something with the class and value
}
}
他のヒント
あなたが持っていると仮定して ネスト スパンタグ、以下は機能するはずです。
/<span(?:[^>]+class=\"(.*?)\"[^>]*)?>(.*?)<\/span>/
基本的なテストのみを行いましたが、スパンタグのクラス(存在する場合)とタグが閉じられるまでデータと一致します。
私 強く 代わりに、実際のHTMLまたはXMLパーサーを使用することをお勧めします。 正規表現でHTMLまたはXMLを確実に解析することはできません- あなたができることは近くに来ることです。そして、あなたが近づくほど、あなたのregexはより複雑で時間がかかるでしょう。解析するための大きなHTMLファイルがある場合、単純なregexパターンを破る可能性が非常に高くなります。
regex like <span[^>]*>(.*?)</span>
あなたの例では動作しますが、正規表現と解析するのが難しいか不可能なXML-validコードがたくさんあります(たとえば、 <span>foo <span>bar</span></span>
上記のパターンを破壊します)。他のHTMLサンプルで動作するものが必要な場合、Regexはここに行く方法ではありません。
HTMLコードはXML-Validではないため、考えてみましょう HTMLアジリティパック, 、私が聞いたことはとても良いです。