我在下面有一个 rtf 格式的文本,其中每个 rtf 始终包含“\par 换行符和括号}”
是否有一个正则表达式可以在任何“}”之前查找和替换/删除最后一次出现的“\par”。
输入文本
{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Consolas;}{\f1\fnil Segoe UI;}}
{\colortbl ;\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;}
{\*\generator Riched20 6.2.9200}\viewkind4\uc1
\pard\ltrpar\tx720\cf1\highlight2\f0\fs19 FocusedTextControl.TextElementReadOnly = \cf3 false\cf1 ;FocusedTextControl.TextElementReadOnly = \cf3 false\cf1 ;FocusedTextControl.TextElementReadOnly = \cf3 false\cf1 ;FocusedTextControl.TextElementReadOnly = \cf3 false\cf1 ;FocusedTextControl.TextElementReadOnly = \cf3 false\cf1 ;\highlight0\f1\fs22\par
}
没有 \par 的所需结果
{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Consolas;}{\f1\fnil Segoe UI;}}
{\colortbl ;\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;}
{\*\generator Riched20 6.2.9200}\viewkind4\uc1
\pard\ltrpar\tx720\cf1\highlight2\f0\fs19 FocusedTextControl.TextElementReadOnly = \cf3 false\cf1 ;FocusedTextControl.TextElementReadOnly = \cf3 false\cf1 ;FocusedTextControl.TextElementReadOnly = \cf3 false\cf1 ;FocusedTextControl.TextElementReadOnly = \cf3 false\cf1 ;FocusedTextControl.TextElementReadOnly = \cf3 false\cf1 ;\highlight0\f1\fs22
}
因为我使用的是 c#,所以看起来这也可以做到:
string parR = @"\par";
var s = content.LastIndexOf(parR, System.StringComparison.Ordinal);
var n = content.Remove(s, parR.Length);
而不是这样做:
Regex r = new Regex(@"\\par\r");
r.Match(content);
var s =r.Replace(content, string.Empty);