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),第二行仅包含字符串。

我只想在<p>标签中查找字符串并将该内容合并到<p>html 文件中的前一个标签。

方法:

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();

以我的形式工作,请参阅沙盒:

于 2013-03-18T06:02:03.197 回答