Click here to Skip to main content
15,942,757 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Iam having a trouble implementing a certain technique - selecting next text in a richtextbox control which is underlined.

You see iam trying to make a word processor but am stuck. I have been able to write the code to select each word one-by-one on pressing F1. Now i want to select only those words which are underlined but can be a phrase rather than single word.

Eg: hello - this is a nice site for everyone

imagine my cursor is at beginning (before hello) and i press F1 (or any button) I want to select "this is a" and on next keypress "site for". But iam not being able to build a regex or implement simple logic for this requirement.

Any help plz? (Any language will do / C#)

1 solution

Run myRichTextBox.Rtf through this:
//  using System.Text.RegularExpressions;
public static Regex regex = new Regex("(?<=\\\\\\\\ul).*(?=\\\\\\\\ulnone)",
    | RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
Share this answer
BobJanova 20-Apr-11 7:44am    
That won't if the RTF does it this way
Here is some {\ul underlined text} in a group

That's the problem with RTF, there are too many ways to do things so it is hard to work with.

Your solution will work with what the current version of the RTF control does when you press the U button, but it won't necessarily work with loaded documents or code-generated RTF snippets.

(Also, surely it would be better to use @ strings for all those backslashes? @"(?<=\\\\ul).*(?=\\\\ulnone)")

Finally, if the user wants to select the underlined text, shouldn't the '.*' be in a bracketed group?
OriginalGriff 20-Apr-11 8:10am    
There are, so you would have to create a regex to handle them as well - stupid specification :laugh: My solution was based on rtf code generates by the using an underlined font to writ ethe text, so it should work for a standard RichTextBox control.
As regards the multiple slashes, I used Expresso to generate and test teh regex, so it was it's choice to not use the @ string prefix! I wasn't about to remove half of them and potential create a problem by counting wrong...
Because the regex uses excluded prefix and suffix groups, you shouldn't need to group the actual content: it should work as is.
BobJanova 20-Apr-11 13:19pm    
Oh, that's what that syntax in the regex is doing :P. I must say, I would write it as
(Actually, isn't that still too many backslashes? I think it should only be 2 after escaping.)

Because groups can be nested, you have to count braces in the latter case. I'm not sure a regex can even do that.
OriginalGriff 20-Apr-11 14:14pm    
No, because you are double escaping: you need to match the text @"\\ul" for start or end of underlining, but '\' is a special in the regex as well, so it needs escaping there also to get a pair of '\' characters into the match string.
BobJanova 20-Apr-11 14:31pm    
RTF only uses single backslashes (@"\ul stuff \ulnone") though. I don't get where the extra level of escape is coming from.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900