我需要使用正则表达式从rtf格式的文本中提取粗体文本。例如:\ b棕色狐狸\ b0跳过了\ b懒狗\ 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);
}

关于c# - 在C#中使用正则表达式匹配rtf格式的文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6577189/

10-11 09:27