c#找出正则表达式匹配的位置并加粗
我使用C#并制作了Win 10通用应用程序 我想把这两场比赛的全部内容都加粗c#找出正则表达式匹配的位置并加粗,c#,regex,win-universal-app,C#,Regex,Win Universal App,我使用C#并制作了Win 10通用应用程序 我想把这两场比赛的全部内容都加粗 Run Run = new Run(); Paragraph Paragraph = new Paragraph(); //RichTextBlock = RTB Run.Text = "D1 abc \n E1 \n F1 \n D1 def"; Paragraph.Inlines.Add(Run); RTB.Blocks.Add(Paragraph); string Patter = "D1"; Regex R
Run Run = new Run();
Paragraph Paragraph = new Paragraph();
//RichTextBlock = RTB
Run.Text = "D1 abc \n E1 \n F1 \n D1 def";
Paragraph.Inlines.Add(Run);
RTB.Blocks.Add(Paragraph);
string Patter = "D1";
Regex Regex = new Regex(Patter);
MatchCollection Mc = Regex.Matches(Run.Text);
foreach (Match Match in Mc)
{
//bold the D1's lines in the RTB
}
输出:
**D1 abc**
E1
F1
**D1 def**
感谢您的帮助和时间假设您的文本位于名为
text
的变量中:
string text = "D1 abc \n E1 \n F1 \n D1 def";
string Patter = "^.*D1.*$";
MatchCollection Mc = Regex.Matches(text, Patter, RegexOptions.Multiline);
int index = 0;
Paragraph.Inlines.Clear();
foreach (Match Match in Mc)
{
//bold the D1's lines in the RTB
Paragraph.Inlines.Add(new Run { Text = text.Substring(index, Match.Index - index) });
var bold = new Bold();
bold.Inlines.Add(new Run { Text = text.Substring(Match.Index, Match.Length) });
Paragraph.Inlines.Add(bold);
index = Match.Index + Match.Length;
}
if (index < text.Length)
{
Paragraph.Inlines.Add(new Run { Text = text.Substring(index) });
}
string text=“D1 abc\n E1\n F1\n D1 def”;
字符串模式=“^.*D1.*$”;
MatchCollection Mc=Regex.Matches(文本、模式、RegexOptions.Multiline);
int指数=0;
段落.Inlines.Clear();
foreach(在Mc中匹配)
{
//在RTB中加粗D1的行
Add(新运行{Text=Text.Substring(index,Match.index-index)});
var bold=new bold();
bold.Inlines.Add(新运行{Text=Text.Substring(Match.Index,Match.Length)});
段落.内联线.添加(粗体);
索引=匹配。索引+匹配。长度;
}
如果(索引<文本长度)
{
Add(新运行{Text=Text.Substring(index)});
}
基本上,我已经修改了正则表达式,使其与整行匹配,而不仅仅是“D1”。然后,我清除段落内容,并根据匹配项添加常规文本和粗体文本。是的,但如何使用该信息将整行加粗。这就是我想知道的事。那么,您的问题是“如何加粗行”还是“如何找到正确的加粗行”?还是“什么是正确的正则表达式”?问题很不清楚。好的,抱歉我更改了它。好的,thx,但是三个“新运行([…])不是vs的acceptet,所以我错在哪里。@DieterMüller我使用WPF编写代码,可能WinRT上没有构造函数。你可以用
new Run{Text=Text.Substring(…)}
Ok thx,sry,我不明白,但是“(new Bold(new Run{[…])”这个“Bold”现在下划线了:/@DieterMüller给我5分钟,我要转换代码:p