读取只包含字符串的行,并将内容合并到前一行

本文关键字:合并 一行 字符串 包含 读取 | 更新日期: 2023-09-27 18:04:30

输入为:

<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>标签中只找到字符串,并将该内容合并到html文件中的前<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]+$");

我该怎么做呢?

读取只包含字符串的行,并将内容合并到前一行

看来我明白你想要达到的目的了:

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: one, two