3

入力は次のとおりです。

<p>1:4 And David said unto him, How went the matter? I pray thee, tell me.</p>

<p>And he answered, That the people are fled from the battle, and many of the people also are fallen and dead; and Saul and Jonathan his son are dead also.</p>

この最初の行には数字 (1:4) が含まれ、2 行目には文字列のみが含まれます。

タグ内の文字列のみを検索し、そのコンテンツをhtml ファイル内の<p>前のタグにマージしたいと考えています。<p>

意味:

1:4 And David said unto him, How went the matter? I pray thee, tell me. And he answered, That the people are fled from the battle, and many of the people also are fallen and dead; and Saul and Jonathan his son are dead also.

私はこのようにすることができます:

Regex.IsMatch(html, @"^[a-zA-Z]+$");

どうやってやるの?

4

1 に答える 1

0

私はあなたが達成しようとしているものを手に入れたようです:

StringBuilder sb = new StringBuilder();
foreach (string line in input.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
{
    sb.Append(line.Trim());

    // notice different regex, i.e.:
    // new paragraph stars with `<p>x:y` and ends with `</p>`

    if (!Regex.IsMatch(line, @"^\<p\>[0-9]\:[0-9].+\</p\>$"))
    {
         sb.AppendLine(); // insert line break
    }
}
string result = sb.ToString();

私からの作品、sandbox: onetwoを参照してください。

于 2013-03-18T06:02:03.197 に答える