开发者

Finding a specific word that might have an RTF tag inside it

I am currently writing a program that formats certain words in a Rich Text document by putting RTF tags around them.

As the program allows for additional formatting rules that can overlap (i.e. colouring the phrase "please help me" all in yellow and colouring "please" in blue), the program has difficulty finding matches that have already been formatted by a previous rule (i.e. "please help me", from the previous example becomes "\cf1 please\cf0 help me", which will not match the other rule.

I have been getting around this by using regular expressions and putting an expression that matches any RTF tag after each character in the phrase (as the rules are defined by the user, and I don't know the specific point of overlap), like this:

line = Regex.Replace(line, @"\bP(?:\\[^ ]* )*l(?:\\[^ ]* )*e(?:\\[^ ]* )*a(?:\\[^ ]*   )*s(?:\\[^ ]* )*e(?:\\[^ ]* )*", Evaluator);

The '(?:\\[^ ]* )*' expression in the above sequence finds the preceding character regardless of any tags after it, but doing this with every rule slows down the code drastically and requires me to generate a regular expression for each one; which may not work as expected depending on the rule.

Err, sorry for the wall of tex开发者_如何学运维t, I'll get to the point now. Does anyone know a more efficient way of finding a word that has RTF tags inside it at an unknown point?


One possibility is to never allow overlap of similar tags - if you need to start formatting the text in red but it's already being formatted in blue, close the blue tag before you start the red tag. Otherwise I'd imagine that the formatting rules could get quite ambiguous.

Edit

If you absolutely need to have overlap, then create a stack of formatting. End the old tag as I said above, but save it to the stack. Start the new tag, and when your new tag is done, if the old formatting was not closed (and is still in the stack) start the old formatting again. Whenever you close a tag you remove it from the stack.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜