简体   繁体   中英

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.

The expression I am using is the following:

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

and the input text (rtf) is included in the screenshot. 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).

You most certainly need to use a different way to parse RTF files; unfortunately I don't know whether the .NET libraries provide an RTF parser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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