我需要使用正则表达式从 rtf 格式的文本中提取粗体文本。例如:\b brown fox\b0 跳过了\b lazy dog\b0。
如何只获取\b 和\b0 之间的文本?我试过这个表达式,但它只返回第一个匹配项:(\\b.+\b0[^\\b])
请您参考如下方法:
string s = @"The \b brown fox\b0 jumped over the \b lazy dog\b0";
Regex rgx = new Regex(@"\\b(.*?)\\b0");
foreach (Match m in rgx.Matches(s))
{
Console.WriteLine(m.Groups[1].Value);
}
或者,您可以使用捕获:
string s = @"The \b brown fox\b0 jumped over the \b lazy dog\b0";
Regex rgx = new Regex(@"(.*?\\b(.*?)\\b0)*");
foreach (Capture c in rgx.Match(s).Groups[2].Captures)
{
Console.WriteLine(c.Value);
}
