Search code examples
regexvb.netstringrichtextboxunderline

select underlined text from rtf using regex


i want to select the next piece of text which is underlined. You see the rtf of a richtextbox has following code for an underlines text :

\ul\i0 hello friend\ulnone\i

But the normal text looks like underlined. What i want to do is on a click of button the rtfbox should select the next piece of text which is underlined. An example piece of text is :

hello [friend your] house [looks] amazing.

imagine the words within square brackets are underlined. When i first click button1 "friend your" should be selected and on next click "looks" should be selected. Kind of keep moving forward and keep selecting it type of application. I know this can be done using regex but can't build a logic.

Any help will be appreciated. Thanks a lot :D


Solution

  • The regex would be

    Dim pattern As String = "\\ul\\i0\s*((?:(?!\\ulnone\\i).)+)\\ulnone\\i"
    

    Explanation

    \\ul\\i0              # the sequence "\ul\i0"
    \s*                   # any number of white space
    (                     # begin group 1:
      (?:                 #   non-capturing group:
        (?!               #     negative look-ahead ("not followed by..."):
          \\ulnone\\i     #       the sequence "\ulnone\i"
        )                 #     end negative look-ahead
        .                 #     match next character (it is underlined)
      )+                  #   end non-capturing group, repeat
    )                     # end group 1 (it will contain all underlined characters)
    \\ulnone\\i           # the sequence "\ulnone\i"