简体   繁体   English

正则表达式问题

[英]Regular Expression question

RegexBuddy shows the matches are OK, but in C# when I try use replace, a semicolon and a curly bracket are not replaced. RegexBuddy 显示匹配正常,但是在 C# 中,当我尝试使用替换时,不会替换分号和大括号。

The expression I am using is the following:我使用的表达式如下:

@"({\\)(.+?)(}+)|(\s?\\)(.+?)(\b)|}$"

and the input text (rtf) is included in the screenshot.并且输入文本 (rtf) 包含在屏幕截图中。 This the code:这是代码:

Regex reg2 = new Regex(@"\\b([\s\S]+?)\\b0");
MatchCollection matches = reg2.Matches(text);

foreach (Match match in matches)
{
    string output = reg.Replace(match.Value, "");
    MessageBox.Show(output);
}

正则表达式好友截图

You are trying to match nested structures with regular expressions.您正在尝试使用正则表达式匹配嵌套结构。 Look at your screenshot: in the first line there are three opening braces and one closing brace, in your third line you have one opening and two closing braces etc.看看你的截图:第一行有三个左大括号和一个右大括号,第三行有一个左大括号和两个右大括号等。

While .NET does provide ways to do nested pattern matching with regexes, your regex is not using them (and it's extremely mystifying to me what exactly you're hoping to achieve).虽然 .NET 确实提供了与正则表达式进行嵌套模式匹配的方法,但您的正则表达式没有使用它们(这对我来说非常神秘,您到底希望实现什么)。

You most certainly need to use a different way to parse RTF files;您肯定需要使用不同的方式来解析 RTF 文件; unfortunately I don't know whether the .NET libraries provide an RTF parser.不幸的是,我不知道 .NET 库是否提供 RTF 解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM