How to extract all the ranges and link content which have link from the RichEditBox in UWP

68 views Asked by At

Imagine I have RichEditBox string length of 100 and have 10 links set to different sets from the string, now how to extract all the ranges of those 10 Links and the contents of the Links. I have set the links for the RichEditBox in this way

RichEditBox.TextDocument.Selection.StartPosition = 10;
RichEditBox.TextDocument.Selection.EndPosition = 16;
RichEditBox.TextDocument.Selection.Link = "\"www.google.com\"";
1

There are 1 answers

2
Junjie Zhu - MSFT On

Here is a solution, use TextGetOptions.FormatRtf to get the text content in Rtf format.

Text -->"This is some sample text", its Rtf format is as follows.

{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil Segoe UI Variable;}}\r\n{\colortbl ;\red0\green0\blue0;\red0\green0\blue255;}\r\n{\\generator Riched20 10.0.22621}\viewkind4\uc1 \r\n\pard\tx720\cf1\f0\fs21 This is so{{\field{\\fldinst{HYPERLINK "www.google.com"}}{\fldrslt{\ul\cf2 me sam}}}}\f0\fs21 ple text\par\r\n}\r\n\0

Then use regular expressions (?<={HYPERLINK).*?(?=}) and (?<=cf2).*?(?=}) to get the links and link content.

public MainPage()
    {
        this.InitializeComponent();
       
        richEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.None, "This is some sample text");
        richEditBox.TextDocument.Selection.StartPosition = 10;
        richEditBox.TextDocument.Selection.EndPosition = 16;
        richEditBox.TextDocument.Selection.Link = "\"www.google.com\"";
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string TextString = null;
        richEditBox.Document.GetText(Windows.UI.Text.TextGetOptions.FormatRtf, out TextString);

        string pattern = @"(?<={HYPERLINK).*?(?=})";
        string input = TextString;

        int count=0;

        foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
        {
           
            Debug.WriteLine("Link{0}: {1}",
                             ++count, match.Value.Trim());
        }

        
        count = 0;
        pattern = @"(?<=cf2).*?(?=})";

        richEditBox.Document.GetText(Windows.UI.Text.TextGetOptions.NoHidden, out TextString);
       
        int searchStart = 0;
        int searchLength = TextString.Length;

        foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
        {

            Debug.WriteLine("Link Content{0}: {1} ",
                             ++count, match.Value.Trim());

            var searchText= TextString.Substring(searchStart, searchLength);

            pattern = @"("+match.Value.Trim()+")";
            foreach (Match posRes in Regex.Matches(searchText, pattern, RegexOptions.IgnoreCase))
            {
                Debug.WriteLine("startPos:{0} - endPos:{1} ",
                             posRes.Index, posRes.Index + match.Value.Trim().Length);
                searchStart = posRes.Index + match.Value.Trim().Length;
                searchLength = TextString.Length - posRes.Index - match.Value.Trim().Length;

            }
                
        }

    }