1

在 C#、Windows 窗体中,我将如何做到这一点:


07:55 Header Text:  This is the data<br/>07:55 Header Text:  This is the data<br/>07:55 Header Text:  This is the data<br/>

所以,如您所见,我有一个返回字符串,它可能相当长,但我希望能够将数据格式化为如下所示:


<b><font color="Red">07:55 Header Text</font></b>:  This is the data<br/><b><font color="Red">07:55 Header Text</font></b>:  This is the data<br/><b><font color="Red">07:55 Header Text</font></b>:  This is the data<br/>

如您所见,我基本上想<b><font color="Red">在标题文本和时间的前面添加,并</font></b>在 : 部分之前添加。

所以是的,哈哈,我有点迷路了。

我已经搞砸了.Replace()Regex模式,但没有取得多大成功。我真的不想替换文本,只是在某些位置附加/预先挂起。

是否有捷径可寻?

注意:[] 标签实际上是 <> 标签,但我不能在这里使用它们 lol

4

4 回答 4

2

仅仅因为您使用 RegEx 并不意味着您必须替换文本。

以下正则表达式:

(\d+:\d+.*?:)(\s.*?\[br/\])

有两个“捕获组”。然后,您可以将整个文本字符串替换为以下内容:

[b][font color="Red"]\1[/font][/b]\2

这应该导致以下输出:

[b][font color="Red"]07:55 Header Text:[/font][/b] This is the data[br/]
[b][font color="Red"]07:55 Header Text:[/font][/b] This is the data[br/]
[b][font color="Red"]07:55 Header Text:[/font][/b] This is the data[br/]

编辑:这是一些演示上述内容的 C# 代码:

var fixMe = @"07:55 Header Text: This is the data[br/]07:55 Header Text: This is the data[br/]07:55 Header Text: This is the data[br/]";
var regex = new Regex(@"(\d+:\d+.*?:)(\s.*?\[br/\])");
var matches = regex.Matches(fixMe);

var prepend = @"[b][font color=""Red""]";
var append = @"[/font][/b]";

string outputString = "";
foreach (Match match in matches)
{
    outputString += prepend + match.Groups[1] + append + match.Groups[2] + Environment.NewLine;
}

Console.Out.WriteLine(outputString);
于 2011-03-24T04:56:19.537 回答
0

你试过.Insert()检查这个

于 2011-03-24T04:53:56.897 回答
0

最简单的方法可能是使用string.Replace()and string.Split()。假设您的输入字符串是input(未经测试):

var output = string.Join("<br/>", in
    .Split("<br/>)
    .Select(l => "<b><font color=\"Red\">" + l.Replace(": ", "</font></b>: "))
    .ToList()
    ) + "<br/>";
于 2011-03-24T04:57:46.680 回答
0

您是否考虑过通过将每一行包装在 apdiv标记中来创建样式并设置每一行的 css 类?

更易于维护和构建。

于 2011-03-24T05:03:22.673 回答