0

HTMLデータを含む文字列変数が1つあります。そのhtml文字列を複数の文字列に分割し、最終的にそれらの文字列を1つにマージしたいと考えています。

これはhtml文字列です:

<p><span style="text-decoration: underline; color: #ff0000;"><strong>para1</strong></span></p>
<p style="text-align: center;"><strong><span style="color: #008000;">para2</span> स्द्स्द्सद्स्द para2 again<br /></strong></p>
<p style="text-align: left;"><strong><span style="color: #0000ff;">para3</span><br /></strong></p>

そして、これは私の期待される出力です:

<p><span style="text-decoration: underline; color: #ff0000;"><strong>para1</strong></span><strong><span style="color: #008000;">para2</span>para2 again<br /></strong><strong><span style="color: #0000ff;">para3</span><br /></strong></p>

私の分割ロジックを以下に示します...

  1. </p>タグに基づいて HTML 文字列をトークンに分割します。
  2. 最初のトークンを取得し、別の文字列変数 (firstPara) に格納します。
  3. 次に、すべてのトークンを取得し、で始まるタグ<pと終わるタグをすべて削除します。そして、</p>各値を個別の変数に保存します。

4.次に、firstPara という名前の最初のトークンを取得し、タグを置き換えて</p>から、手順 3で取得したすべてのトークンを追加します。

5.これで、変数 firstPara は完全な値を持ちます...

  1. 最後に</p>、firstPara の最後に追加するだけです...

これは私の問題です...

この問題から抜け出すために私にステップを踏んでもらえますか...

4

2 に答える 2

1

これを行う方法の正規表現の例を次に示します。

String pattern = @"(?<=<p.*>).*(?=</p>)";
var matches = Regex.Matches(text, pattern);
StringBuilder result = new StringBuilder();
result.Append("<p>");
foreach (Match match in matches)
{
    result.Append(match.Value);
}
result.Append("</p>");

そして、これはHtml Agility Packでそれを行う方法です

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(text);
var nodes = doc.DocumentNode.SelectNodes("//p");
StringBuilder result = new StringBuilder();
result.Append("<p>");
foreach (HtmlNode node in nodes)
{
    result.Append(node.InnerHtml);
}
result.Append("</p>");
于 2012-12-14T09:21:11.400 に答える
1

stringaを別ので分割したい場合は、 where isstringを使用できます。この配列には、分割に使用される文字列が少なくとも 1 つ含まれています。string.Split(string[] separator, StringSplitOptions options)separatorstringstring

//Initialize a string of name HTML as our HTML code
string HTML = "<p><span style=\"text-decoration: underline; color: #ff0000;\"><strong>para1</strong></span></p> <p style=\"text-align: center;\"><strong><span style=\"color: #008000;\">para2</span> स्द्स्द्सद्स्द para2 again<br /></strong></p> <p style=\"text-align: left;\"><strong><span style=\"color: #0000ff;\">para3</span><br /></strong></p>";
//Initialize a string array of name strSplit to split HTML with </p>
string[] strSplit = HTML.Split(new string[] { "</p>" }, StringSplitOptions.None);
//Initialize a string of name expectedOutput
string expectedOutput = "";
string stringToAppend = "";
//Initialize i as an int. Continue if i is less than strSplit.Length. Increment i by 1 each time you continue
for (int i = 0; i < strSplit.Length; i++)
{
    if (i >= 1) //Continue if the index is greater or equal to 1; from the second item to the last item
    {
        stringToAppend = strSplit[i].Replace("<p", "<"); //Replace <p by <
    }
    else //Otherwise
    {
        stringToAppend = strSplit[i]; //Don't change anything in the string
    }
    //Append strSplit[i] to expectedOutput
    expectedOutput += stringToAppend;
}
//Append </p> at the end of the string
expectedOutput += "</p>";
//Write the output to the Console
Console.WriteLine(expectedOutput);
Console.Read();

出力

<p><span style="text-decoration: underline; color: #ff0000;"><strong>para1</stro
ng></span> < style="text-align: center;"><strong><span style="color: #008000;">p
ara2</span> ?????????????? para2 again<br /></strong> < style="text-align: left;
"><strong><span style="color: #0000ff;">para3</span><br /></strong></p>

注意: 私のプログラムは Unicode 文字をサポートしていないため、 を読み取ることができませんでしस्द्स्द्सद्स्दた。ということで、と訳されまし??????????????た。

ありがとう、
これがお役に立てば幸いです:)

于 2012-12-14T09:24:26.217 に答える